我有一个在c#(Visual Studio 2010)中开发的Windows应用程序,它与连接到PC的USB端口的USB-CAN转换器进行交互。我在一个线程上实现了一个UI,在一个单独的线程上实现了发送和接收逻辑。完成我的任务后,我想关闭此线程并返回到当前挂起的UI线程!C#多线程问题
我试图用abort函数强制杀死线程,但它会抛出异常。那么在完成我的任务后如何优雅地结束这个线程。
我有以下2个问题:
- 如何优雅地终止线程并返回到UI线程?
- 为什么这个传输和接收线程不会中止并挂起UI线程?
下面的代码是供你参考:
#region recievedthrad
public void ThreadProcReceive()
{
try
{
bool fDoExit = false;
do
{
if (bFlag)
DeleteAllResources(); // This will free all the driver resources.
// wait for receiving messages or exiting this thread
/* Note: After completion of the task, this method needs to return a 0 or 2 value. But still it returns 1 or 258 and never exits the thread. */
int index = WaitHandle.WaitAny(m_RxWaitHandles);
if (index == 0)
{
// exit this thread
fDoExit = true;
}
else if (index == 1)
{
// receive all messages
ReceiveAllCanMessages();
}
else if (index == 2)
{
// exit this thread
ReceiveStatus(UcanDotNET.USBcanServer.USBCAN_CHANNEL_CH0);
}
} while ((fDoExit == false));
}
catch(Exception ex)
{
}
}
#endregion
我很困惑。你有GUI线程和其他线程做USB/CAN通信。为什么GUI线程挂起?如果挂起是因为你试图指示通信线程终止并等待它的句柄,(Thread.Join),那么就不要这样做!指出它完成,好吧,只是不要等待它。无论如何,你是否需要它 - 为什么不把它放在周围呢? – 2012-04-05 11:56:13
这可能是一个使用BackgroundWorker的好地方 – 2012-04-05 11:59:54
在'index == 2'的情况下,你有一个注释'//退出这个线程'但是你没有设置'fDoExit = true'。 – Henrik 2012-04-05 12:19:29