如何在多线程中释放线程? [英] how to release thread is required in multiple thread ?
问题描述
大家好!
作为标题,如何在多个线程中释放线程?
例如:我有5个线程正在等待。我只希望线程位置3被释放
我使用autoresetevent / manualresetevent / monitor.wait和monitor.pulse但所有发布线程都遵循FIFO
帮助我!!! br />
更新:
这是form1:
Hi everybody !
As title, how to release thread is required in multiple thread ?
Ex : I have 5 thread is waiting. I only want thread position 3 is released
I use autoresetevent/manualresetevent/monitor.wait and monitor.pulse but all release thread follow FIFO
help me !!!
UPDATED:
This is form1:
private BackgroundWorker[] threadArray;
public static ManualResetEvent _manualResetEvent = new ManualResetEvent(false);
private void btn_Start_Scraping_Click(object sender, EventArgs e)
{
threadArray = new BackgroundWorker[listView_Site.Items.Count];
for (var f = 0; f < listView_Site.Items.Count; f++)
{
threadArray[f] = new BackgroundWorker();
threadArray[f].DoWork += new DoWorkEventHandler(BackgroundWorkerFilesDoWork);
threadArray[f].RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundWorkerFilesRunWorkerCompleted);
threadArray[f].ProgressChanged += new ProgressChangedEventHandler(BackgroundWorkerFilesProgressChanged);
threadArray[f].WorkerReportsProgress = true;
threadArray[f].WorkerSupportsCancellation = true;
threadArray[f].RunWorkerAsync(listView_Site.Items[f].Tag.ToString());
}
}
private void BackgroundWorkerFilesDoWork(object sender, DoWorkEventArgs e)
{
....// all above code is fine
//at here, I call waitOne() to wait when user enter and get text of textbox from form
_manualResetEvent.WaitOne();
//textCaptcha is received from form2
string textCaptcha=clsValueStatic.CaptchaText;
string postdata=loginparameter + textCaptcha;
requestCaptcha = (HttpWebRequest)WebRequest.Create(uriActionLoginPage);
requestCaptcha.Pipelined = true;
requestCaptcha.KeepAlive = true;
requestCaptcha.AllowAutoRedirect = false;
requestCaptcha.Timeout = 60000;
requestCaptcha.CookieContainer = sessionID;
request.ServicePoint.Expect100Continue = false;
requestCaptcha.Method = "GET";
}
Form2:
Form2:
private void textBoxCaptcha_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
var textBox = sender as TextBoxX;
if (textBox != null)
{
clsValueStatic.CaptchaText = textBox.Text.Trim();
textBox.Parent.Parent.Dispose();
frmScrapingAnalysis._manualResetEvent.Set();
}
}
}
PS:Form1有1个按钮启动多个backgroundworker并显示form2然后所有backgroundworker等待从form2获取文本盒的文本验证码
当用户输入backgroundworker的文本时,我想要在form2上显示然后只释放backgroundworker。所有其他背景工作者仍在等待
PS : Form1 have 1 button to start multiple backgroundworker and show form2 then all backgroundworker wait to get text captcha of textbox from form2
my way want when user enter text of backgroundworker is shown on form2 then only the backgroundworker is released. All other backgroundworker still wait
推荐答案
明显的解决方案可能很简单。在您的FIFO中,不仅存储线程,而且为每个线程分别提供System.Threading.EventWaitHandle
,System.Threading.Monitor
,而不是一个线程实例,在你的FIFO中保存组成这个实例的记录与EventWaitHandle
或Monitor
实例,并分别限制每个线程。
这是解决方案。但它有意义吗?我对此表示怀疑。看起来整个方法可能几乎没有任何意义。
这就像有一些线程,但没有并行操作。当您一次允许一个非等待线程时,可能会发生这种情况。为什么要打败线程的目的?
The apparent solution could be simple. In your FIFO, store not just threads but give each thread its separate instance ofSystem.Threading.EventWaitHandle
,System.Threading.Monitor
, instead of a thread instance, keep in your FIFO the record composing this instance with theEventWaitHandle
orMonitor
instance, and throttle each thread separately.
This is the solution. But does it makes sense? I doubt it. It looks like you whole approach may make little to no sense.
This is like having a number of thread, but without parallel operation. It may happen when you allow one non-waiting thread at a time. Why defeating the purpose of threading then?
但我正在考虑一个计划
要染一个绿色的胡须,
总是使用这么大的粉丝
它无法看到。
But I was thinking of a plan
To dye one's whiskers green,
And always use so large a fan
That it could not be seen.
你可以通过排队任务而不是线程来做同样的事情。例如,您可以在阻塞队列中对委托实例进行排队。有关进一步说明,请参阅我的文章:用于线程通信的简单阻塞队列和线程间调用 [ ^ ]。
You can have the same thing by queuing tasks instead of threads. For example, you can queue delegate instances in a blocking queue. For further explanation, please see my article: Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^].
这篇关于如何在多线程中释放线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!