0

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

Cornor
  • 33
  • 2
  • 9
  • Your question is too vague. There are plenty of ways to accomplish what you want - the simplest is to just have an array of sync objects and signal the one for the thread you want to release - but you haven't told us anything about your code. It's impossible to tell you how to address the issue in your own code. Post a good, complete, concise code example. See http://stackoverflow.com/help/mcve The answer will in general involve some means of thread identification and signaling a specific thread, and may or may not involve an individual object per thread, depending on your exact requirements. – Peter Duniho Nov 16 '14 at 07:54
  • ok guy! I updated my code. please, help me! I got 3 days but this problem can't solve – Cornor Nov 16 '14 at 07:59
  • First, your `DoWork` event handler is too complicated. You will have an easier time writing and maintaining the code if you factor it properly, with the work broken down into smaller methods. But more to the point, you haven't posted a good code example. There's lots of code there that has nothing at all to do with the question, and yet the example itself is not complete. Please review the link I provided previously. – Peter Duniho Nov 16 '14 at 20:08

0 Answers0