我有一个客户端,它创建一个线程。ManualResetEvent不起作用;线程
该线程有一个WaitOne()
所以当它卡在那里时,我的客户端不会死。 但是,当我想关闭我的客户端时,我需要在该手动重置事件上执行Set()
。
我宣布在主类的手动重置事件:
public ManualResetEvent mreIn = new ManualResetEvent(false);
这是我Connect
函数创建具有启动功能的线程:
public void Connect()
{
objClientThread = new Thread(start) { IsBackground = true };
objClientThread.Start();
}
/// <summary>
/// Starts the client program.
/// </summary>
private void start()
{
//We Open the proxy to let connections happen
objProxy.Open();
if (performHandshake())
{
IsConnected = true;
DelayedShutdownBool = false;
//While connected, the thread keeps the client alive
mreIn.WaitOne();
if (OnShutdownInitiated != null)
{
OnShutdownInitiated(this, new EventArgs());
}
System.Threading.Thread.Sleep(500);
objProxy.Close();
objConfiguration = null;
IsConnected = false;
mreOut.Set();
}
}
而且我有一个回调哪些呢Set()
:
Boolean IServiceCallbackContract.Shutdown()
{
mreIn.Set();
return true;
}
所以方法th是工作是... 所有模块被初始化和封锁在WaitOne()
当我关闭一个模块,回调做Set()
但WaitOne()
未解锁,线程不会继续。 我错过了什么?
首先检查:回调是否在同一个实例上运行? –
是的,回调函数运行的是同一个实例。客户端是用executeAssembly执行的'.exe'。这个'.exe'有一个客户类的实例。当'exe'启动时,调用Connect()函数,创建线程 – AAlferez
但是你的objProxy看起来像一个错误的主机。 –