Problem with remotly turn on/off monitor

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emhiil
    New Member
    • Jan 2009
    • 4

    Problem with remotly turn on/off monitor

    Hi!

    I'm trying to remotly turn on/off my monitor using this code below. The problem is whenever I call the method that turn on/off the monitor from within the tcp asynchronous receive data method, the application just ends with no exception. So i put in a delegate but the same problem was still there.

    What is the correct way of doing this?

    Code:
    public delegate void SendMessageCallback(int hWnd, int hMsg, int wParam, int lParam);
    
    public int WM_SYSCOMMAND = 0x0112;
    public int SC_MONITORPOWER = 0xF170;
    
    [DllImport("user32.dll")]
    private static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);
    Code:
    public void OnDataReceived(IAsyncResult asyn)
    {
    	SocketPacket socketData = (SocketPacket)asyn.AsyncState;
    
    	try
    	{
    		int iRx  = socketData.currentSocket.EndReceive(asyn);
    		char[] chars = new char[iRx + 1];
    
    		Decoder d = Encoding.UTF8.GetDecoder();
    		int charLen = d.GetChars(socketData.dataBuffer, 0, iRx, chars, 0);
    
    		String szData = new String(chars);
    
    		if (szData.ToString().Substring(0, 4) == "1040")
    	    {
    			SendMonitorMessage(this.Handle.ToInt32(), WM_SYSCOMMAND, SC_MONITORPOWER, 2);
    		}
    	}
    	catch { }
    }
    Code:
    private void SendMonitorMessage(int hWnd, int hMsg, int wParam, int lParam)
    {
    	if (InvokeRequired)
    	{
    		this.Invoke(new SendMessageCallback(this.OnSendMonitorMessage), new object[] { hWnd, hMsg, wParam, lParam });
    	}
    	else
    	{
    		this.OnSendMonitorMessage(hWnd, hMsg, wParam, lParam);
    	}
    }
    
    private void OnSendMonitorMessage(int hWnd, int hMsg, int wParam, int lParam)
    {
    	SendMessage(hWnd, hMsg, wParam, lParam);
    }
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Did you try using breakpoints an look whats happening?
    Maybe you also take a look into window's error-log/event-viewer.

    Comment

    Working...