Backgroundworker with EventWaitHandle no respond????
-
I am writing a windows application to upload file by using Backgroundworker and EventWaitHandle. But it doesn't get any respond after run the line 'handle.WaitOne();'. Can anyone help?
private void btnUpload_Click(object sender, EventArgs e) { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); MessageBox.Show("FTP 1"); worker.RunWorkerAsync(); MessageBox.Show("FTP 2"); handle.WaitOne(); // This blocks main thread MessageBox.Show("FTP 3"); } static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { MessageBox.Show("FTP 4"); handle.Set(); }
-
I am writing a windows application to upload file by using Backgroundworker and EventWaitHandle. But it doesn't get any respond after run the line 'handle.WaitOne();'. Can anyone help?
private void btnUpload_Click(object sender, EventArgs e) { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); MessageBox.Show("FTP 1"); worker.RunWorkerAsync(); MessageBox.Show("FTP 2"); handle.WaitOne(); // This blocks main thread MessageBox.Show("FTP 3"); } static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { MessageBox.Show("FTP 4"); handle.Set(); }
One of the nice things about
BackgroundWorker
is thatRunWorkerCompleted
is raised on the UI thread. In your example, you have blocked the UI thread with the call tohandle.WaitOne()
, soworker_RunWorkerCompleted
can never run and signal yourhandle
:wtf: Also, the point ofBackgroundWorker
is to avoid blocking the UI thread. Why are you blocking it? Nick---------------------------------- Be excellent to each other :)