How to wait and get text from another form in multiple backgroundworker

霸气de小男生 提交于 2019-12-11 15:24:51

问题


This is 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());


            }
        }

This is 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();
        }                
    }
}

this is program to login website

  • 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
  • I try to code and debug with AutoResetEvent/ManualResetEvent and Monitor.Wait()/Pulse() and EventWaitHandle but order execution of all backgroundworker is FIFO
  • 3 days ago, I still have not been anyway to solve this problem :(
  • Or other anyway can solve my problem. Thank you so much

来源:https://stackoverflow.com/questions/26467732/how-to-wait-and-get-text-from-another-form-in-multiple-backgroundworker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!