Question

I have a program which allows users to log into an entire computer lab automatically using VNCSharp. After some code reorganization I am running into an issue I cant seem to solve. The following method logs into the computer after a VNC connection has been established.

        private void rd_ConnectComplete_1(object sender, ConnectEventArgs e)
       {
        if (Action == LabLoginAction.Login)
        {
            //give the remote desktop control focus
            rd.Focus();

            //send a control alt delete
            rd.SendSpecialKeys(SpecialKeys.CtrlAltDel);

            //wait 1/2 second
            System.Threading.Thread.Sleep(500);

            //run through the secure windows user name string
            foreach (char c in ConvertToUnsecureString(WindowsUsername))
            {
                //type each key, then wait 1ms
                SendKeys.SendWait(c.ToString());
                System.Threading.Thread.Sleep(1);
            }

            //send a tab key press
            SendKeys.SendWait("{TAB}");

            //run through the secure windows password string
            foreach (char c in ConvertToUnsecureString(WindowsPassword))
            {
                //type each key, then wait 1ms
                SendKeys.SendWait(c.ToString());
                System.Threading.Thread.Sleep(1);
            }

            //send an enter key press
            SendKeys.SendWait("{ENTER}");

            //wait 1ms
            System.Threading.Thread.Sleep(1);

            //disconect from the remote computer
            rd.Disconnect();
        }
    }

This method logs the computer in successfully however, once rd.Disconnect() is called at the end, it throws a null reference exception. The rd object represents a VNCSharp RemoteDesktop control which I have on my form. How is it throwing a null reference exception when calling the disconnect method, but not when its used previously in my method?

Was it helpful?

Solution

As Evan L suggested there were two properties that were null that are used in the RemoteDesktop disconnect method. Somehow, they were not getting initialized until after the disconnect method was called. Both of these objects had to do with a worker thread which is used to get incoming updates from the server. However, for my application I only need to login to the computer, disconnect and move on to the next machine. Therefore I have no need of updates for any kind of desktop control. So I simply modified to code to not try and stop the thread, as it doesn't exist yet.

public void Disconnect()
    {
        // Stop the worker thread.
        if (done != null)
        {
            done.Set();
        }

        // BUG FIX: Simon.Phillips@warwick.ac.uk for UltraVNC disconnect issue
        // Request a tiny screen update to flush the blocking read
        try {
            rfb.WriteFramebufferUpdateRequest(0, 0, 1, 1, false);
        } catch {
            // this may not work, as Disconnect can get called in response to the
            // VncClient raising a ConnectionLost event (e.g., the remote host died).
        }

        if (worker != null)
        {
            worker.Join(3000);  // this number is arbitrary, just so that it doesn't block forever....
        }

        rfb.Close();    
        rfb = null;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top