how to release thread is required in multiple thread?

416 views Asked by At

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
           requestCaptcha = (HttpWebRequest)WebRequest.Create(uriCaptchaPage);
                            requestCaptcha.Pipelined = true;
                            requestCaptcha.KeepAlive = true;
                            requestCaptcha.AllowAutoRedirect = false;
                            //request.Proxy = null;

                    requestCaptcha.Timeout = 60000;
                    requestCaptcha.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                            requestCaptcha.CookieContainer = sessionID;
                            request.ServicePoint.Expect100Continue = false;

                            requestCaptcha.Method = "GET";
                            requestCaptcha.Referer = uriloginPage.AbsoluteUri;

                            //get response.
                            responseCaptcha = (HttpWebResponse)requestCaptcha.GetResponse();
                            Stream imagestream = responseCaptcha.GetResponseStream();
                            if (imagestream != null)
                            {
                                Image image = Image.FromStream(imagestream);

                                if (Directory.Exists(Application.StartupPath + "\\Captcha") == false)
                                {
                                    Directory.CreateDirectory(Application.StartupPath + "\\Captcha");
                                }

                                switch (responseCaptcha.ContentType)
                                {
                                    case "image/jpeg":
                                    {
                                        saveLocation += ".jpg";
                                        if (File.Exists(saveLocation))
                                        {
                                            File.Delete(saveLocation);
                                        }
                                        image.Save(saveLocation,ImageFormat.Jpeg);
                                        break;
                                    }
                                    case "image/gif":
                                    {
                                        saveLocation += ".gif";
                                        if (File.Exists(saveLocation))
                                        {
                                            File.Delete(saveLocation);
                                        }
                                        image.Save(saveLocation, ImageFormat.Gif);
                                        break; 
                                    }
                                    case "image/png":
                                    {
                                        saveLocation += ".png";
                                        if (File.Exists(saveLocation))
                                        {
                                            File.Delete(saveLocation);
                                        }
                                        image.Save(saveLocation, ImageFormat.Png);
                                        break; 
                                    }
                                }

                               //show form2 to enter captcha
                               lock (_lockObj)
                                {
                                    if (Application.OpenForms.OfType<frmCaptchaQuestion>().Any() == false)
                                    {
                                        DoOnUIThread(delegate()
                                        {
                                            _formCaptchaQuestion.CreatePanelCaptcha(uriloginPage, saveLocation,idHomePage);
                                            _formCaptchaQuestion.Show();
                                        });
                                    }
                                    else
                                    {
                                        DoOnUIThread(() => _formCaptchaQuestion.CreatePanelCaptcha(uriloginPage, saveLocation,idHomePage));
                                    }

                                }

                                //wait and get captcha from form2 and only run thread is required
                                //this is my problem <<<<========================================
                                lock (_lockObj)
                                {
                                    //_manualResetEvent.WaitOne(30000);
                                    //_manualResetEvent.Reset();
                                    //if (clsValueStatic.CaptchaText != null)
                                    //{
                                    //    foreach (var id in clsValueStatic.CaptchaText)
                                    //    {
                                            while (!_go)
                                            {
                                                Monitor.Wait(_lockObj);
                                            }
                                    //    }
                                    //}

                                }


                                requestCaptcha = (HttpWebRequest)WebRequest.Create(uriActionLoginPage);
                                requestCaptcha.Pipelined = true;
                                requestCaptcha.KeepAlive = true;
                                requestCaptcha.AllowAutoRedirect = false;
                                //request.Proxy = null;

                                requestCaptcha.Timeout = 60000;
                                requestCaptcha.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
                                requestCaptcha.CookieContainer = sessionID;
                                request.ServicePoint.Expect100Continue = false;

                                requestCaptcha.Method = "GET";
        }

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 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

0

There are 0 answers