如何在多个backgroundworker中等待并从另一个表单获取文本

时间:2014-10-20 14:12:05

标签: multithreading winforms httpwebrequest backgroundworker

这是Form1

public static BackgroundWorker[] threadArray;
//public static AutoResetEvent[] _eventHandles ;
public static readonly object _lockObj = new object();
public static bool _go ;
//public static ManualResetEvent _manualResetEvent = new ManualResetEvent(false);

private void BackgroundWorkerFilesDoWork(object sender, DoWorkEventArgs e)
{
  ...
    lock (_lockObj)
    {
    //_manualResetEvent.WaitOne(30000);
    //_manualResetEvent.Reset();
    //if (clsValueStatic.CaptchaText != null)
    //{
    //    foreach (var id in clsValueStatic.CaptchaText)
    //    {
               while (!_go)
               {
                   Monitor.Wait(_lockObj);
               }
     //    }
     //}

     }
...
}

        private void btn_Start_Scraping_Click(object sender, EventArgs e)
        {
            ServicePointManager.DefaultConnectionLimit = slider_Thread.Value;
            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());


            }
        }

这是Form2

private void textBoxCaptcha_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        var textBox = sender as TextBoxX;
        if (textBox != null)
        {
            lock (frmScrapingAnalysis._lockObj)
            {
                //clsValueStatic.CaptchaText = null;
                ////frmScrapingAnalysis._go = true;
                //clsValueStatic.CaptchaText.Add(textBox.Tag.ToString(), textBox.Text.Trim());
                //textBox.Parent.Parent.Dispose();
            Monitor.Pulse(frmScrapingAnalysis._lockObj);
            }

            //frmScrapingAnalysis._manualResetEvent.Set();

            //frmScrapingAnalysis._eventHandles[frmScrapingAnalysis.positionThread].Set();
        }                
    }
}

这是登录网站的程序

  • Form1有1个按钮来启动多个backgroundworker并显示form2然后所有backgroundworker等待从form2获取文本盒的文本验证码
  • 我的方式想要当用户输入backgroundworker的文本显示在form2上时,只有backgroundworker被释放。所有其他背景工作者仍在等待
  • 我尝试使用AutoResetEvent / ManualResetEvent和Monitor.Wait()/ Pulse()和EventWaitHandle进行编码和调试,但是所有后台工作者的命令执行都是FIFO
  • 3天前,我仍然没有解决这个问题:(
  • 或者其他无论如何都可以解决我的问题。非常感谢你

0 个答案:

没有答案