我尝试了以下(伪代码),但当Iam尝试阻止我的线程时,我总是遇到死锁。 问题是Join()等待线程完成,并且一个挂起的Invoke()操作也在等待完成。我该如何解决这个问题?如何正确地停止正在使用Control.Invoke的线程
Thread workerThread = new Thread(BackupThreadRunner);
volatile bool cancel;
// this is the thread worker routine
void BackupThreadRunner()
{
while (!cancel)
{
DoStuff();
ReportProgress();
}
}
// main thread
void ReportProgress()
{
if (InvokeRequired)
{
Invoke(ReportProgress);
}
UpdateStatusBarAndStuff();
}
// main thread
void DoCancel()
{
cancel=true;
workerThread.Join();
}