How do I know BackgroundWorker work complete?
-
I would like to upload file one by one using BackgroundWorker. But it seems the application using multi-thread to uplaod. Is there any methods like Thread.join on backgroundWorker? Thanks, Mimi
-
I would like to upload file one by one using BackgroundWorker. But it seems the application using multi-thread to uplaod. Is there any methods like Thread.join on backgroundWorker? Thanks, Mimi
Do you want to wait till background worker finishes? Use waithandles. Call
WaitOne()
when process starts. HandleRunWorkerCompleted
event and signal your waithandle.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
Do you want to wait till background worker finishes? Use waithandles. Call
WaitOne()
when process starts. HandleRunWorkerCompleted
event and signal your waithandle.All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
can you show me the example how waithandles work with BackgroundWorker. Thanks.
-
can you show me the example how waithandles work with BackgroundWorker. Thanks.
Implementing waithandles is trivial. Here is a console application.
class Program{
static EventWaitHandle handle = new AutoResetEvent(false);
static void Main(string[] args) {
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();Console.WriteLine("Main thread waiting"); **handle.WaitOne();** // This blocks main thread Console.WriteLine("Main thread released."); Console.Read(); } static void worker\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Console.WriteLine("Worker finished. Releasing main thread"); **handle.Set();** } static void worker\_DoWork(object sender, DoWorkEventArgs e) { Thread.Sleep(1000); // Simulate a long running job }
}
Hope it is clear now
All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions
-
I would like to upload file one by one using BackgroundWorker. But it seems the application using multi-thread to uplaod. Is there any methods like Thread.join on backgroundWorker? Thanks, Mimi
That's because a BackgroundWorker is a thread. If you want to use a thread to upload a file (presumably to keep the UI responsive during the upload), a join is pointless. Set your backgroundworker to report its progress, add the appropriate event handler, and add a control that's updated when the backgroundworker reports its progress. If you want the program to not allow input during the upload, don't use a thread (or async upload).
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001