我有一些线程正在运行,一旦检测到错误,我想使用消息框之类的交互来继续执行或停止它。而且我不想在我的屏幕上出现多个 msgbox,所以我添加了一个信号量,只让 1 个线程完成这项工作。但它不起作用。所以我有以下情况:
private void DO_WORK()
{
//some code missing
lock (_threadLock)
{
++activeWorkers;
}
ThreadPool.QueueUserWorkItem(o =>
{
WorkRequests(result);
lock (_threadLock)
{
--activeWorkers;
Monitor.Pulse(_threadLock);
}
});
lock (_threadLock)
{
if (STOP)
break;
while (activeWorkers > THREADS)
Monitor.Wait(_threadLock);
}
}
private void WorkRequests(string mystr)
{
string source = null;
string result = null;
bool processed = false;
bool messageBoxShown = false;
///////////////////////////////////
while(true)//this is for rechecking the bad ones
{
source = GetSource(mystr);
if (source == "ERROR_LIMITED")
{
lock (_threadLock)
{
if (!messageBoxShown)//<--- check messageBoxShown
{
if (MessageBox.Show("Blocked IP detected!\nPlease change it!", "test program", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
messageBoxShown = true; //<--- set to true
}
else
DoStop();
}
}
}
else
break;
}
result = mystr + " - " + source;
////////////////////////////////////////
}
如何暂停所有线程,除了将显示消息框并基于对话框继续执行或停止它的线程?