我有一个程序,允许用户使用VNCSharp自动登录整个计算机实验室。经过一些代码重组后,我遇到了一个似乎无法解决的问题。建立VNC连接后,以下方法将登录到计算机中。
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();
}
}
此方法成功记录计算机,但是一旦调用rd.Disconnect(),它将抛出空引用异常。 rd对象代表我在表单上的VNCSharp RemoteDesktop控件。调用disconnect方法时如何抛出空引用异常,而不是在我之前在我的方法中使用它时?
答案 0 :(得分:1)
正如Evan L所说,在RemoteDesktop断开连接方法中使用了两个null属性。不知何故,直到调用disconnect方法之后,它们才被初始化。这两个对象都与工作线程有关,工作线程用于从服务器获取传入更新。但是,对于我的应用程序,我只需要登录到计算机,断开连接并转到下一台机器。因此,我无需更新任何类型的桌面控件。所以我只是修改为代码而不是尝试停止线程,因为它还没有存在。
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;
}