Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
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:
C#
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:
C#
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 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
Posted
Updated 10-Oct-14 7:08am
v2
Comments
Sergey Alexandrovich Kryukov 10-Oct-14 11:32am    
Not clear. Are they waiting at the same instance of EventWaitHandle or Monitor? Why? What's the problem?
And why would you need such selective release? You should better describe your code design (to critically review it), and your goals. Without knowing your goals, it's try to advise anything reasonable.
—SA
sergio090588 10-Oct-14 13:09pm    
I updated my code

1 solution

The apparent solution could be simple. In your FIFO, store not just threads but give each thread its separate instance of System.Threading.EventWaitHandle, System.Threading.Monitor, instead of a thread instance, keep in your FIFO the record composing this instance with the EventWaitHandle or Monitor 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?
It resembles me the idea of Lewis Carroll's White Knight, in "The White Knight's Song":

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[^].

—SA
 
Share this answer
 
v3
Comments
sergio090588 11-Oct-14 8:58am    
I updated my code
Sergey Alexandrovich Kryukov 11-Oct-14 20:23pm    
Thank you. If your question about releasing a select thread remains the same, my answer remains the same.
—SA
sergio090588 12-Oct-14 8:51am    
your aticle that you posted.I try to read but can't understand. Now, i still can not solve my issue :(
Sergey Alexandrovich Kryukov 12-Oct-14 12:15pm    
Then perhaps you have to be more specific. You could try to explain your ultimate goal. This is about the design of your threaded code, a delicate thing. Same thing with understanding of the article: what exactly is unclear?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900