0

如题,多线程需要如何释放线程?例如:我有 5 个线程正在等待。我只想释放线程位置 3 我使用 autoresetevent/manualresetevent/monitor.wait 和 monitor.pulse 但所有释放线程都遵循 FIFO 帮助我!!!

更新:这是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";
        }

表格2:

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被释放。所有其他后台工作人员仍在等待

4

0 回答 0