C# calling backgroundWorker from another thread than UI thread?

There's a lot of things wrong with your code. If you can, try to take a step back and describe what exactly you want to do.

There's a lot of things wrong with your code. If you can, try to take a step back and describe what exactly you want to do. BackgroundWorker uses the Event-based Asynchronous Pattern (EAP).

As such, it requires a thread context in which to live. UI threads satisfy this requirement, but manually-created Thread instances do not (unless you install one or make the instance a secondary UI thread). Similarly, UI components bind to a particular thread.

They require an STA thread that does message pumping (e.g. , Application. DoEvents). It looks to me like you're creating a manual Thread and then creating UI components from that thread (so you know that the thread should be STA and include a message pumping loop, neither of which are in your code).

Then that thread starts a BGW which does message pumping. It's not clear what you're trying to accomplish here - maybe displaying a dialog in a separate thread? Multiple UI threads in a WinForms app is not an officially supported scenario AFAIK, though some people have gotten it working.

I've never seen a need for it, though.

Thanks for your answer. What I'm trying to do is to show a small form which has a marquee progress bar in a thread other than UI thread (as the code states above, this small form is shown in mainThread). Because if I don't use another thread to show this form, the marquee process bar won't be loaded.

That's where my problem starts. Do you know of a way to load this small form with process bar? – PeteMerry Dec 22 '10 at 21:05 I think you're trying to solve this the wrong way.

Instead of blocking your main UI thread by doing some operation and creating a second UI thread to show the small progress form, break out the operation into a separate thread (e.g. , by using BackgroundWorker). From your main UI thread you can then start the background operation, show the small progress form (modally), and have the BackgroundWorker. RunWorkerCompleted event close the small progress form.

– Stephen Cleary Dec 22 '10 at 21:27 But after the small progress form is closed, the operation still need to keep on running. Will the backgroundWorker. RunWorker Completed also stop other operation which runs on backgroundWorker?

And the event load small progress form is called in the operation (as above code it is called in side listentoServer, which will be on another thread if I use backgroundWorker instead of mainThread). Can I use UI thread to load the small progress form in the operation? – PeteMerry Dec 22 '10 at 21:46 Each BackgroundWorker is independent.

If you need the small progress form to remain open, then don't close it in RunWorkerCompleted. The general guideline is as follows: 1) Keep all your UI access and forms in a single thread; 2) Use BackgroundWorker (or Task, etc) for independent background tasks. – Stephen Cleary Dec 22 '10 at 16:28.

According to what you have shown (which is admittedly incomplete, so this may not be the problem), you are not hooking up your event to the backgroundWorker_DoWork and backgroundWorker_RunWorkerCompleted event handlers. Somewhere (after you instantiate your backgroundWorker), you should have this: backgroundWorker. DoWork += new EventHandler(backgroundWorker_DoWork); backgroundWorker.

RunWorkerCompleted += new EventHandler(backgroundWorker_RunWorkerCompleted); As a disclaimer, this was written by hand, so the event names or EventHandler types may be incorrect.

Thanks for your answer, these 2 lines of codes are already there in Window Form Designer generate code. That's why I could call the DoWork but I don't know how to to call RunWorkCompleted. – PeteMerry Dec 22 '10 at 20:47.

I really don't know how to fix your code definitively, or if your code even works the way you have it, I can only give you the following guidance. Use CancellationPending property of background worker, not the IsBusy property when working with windows forms and threaded code, always use the Invoke/BeginInvoke methods to make sure you marshal your call back to the thread that the control originated from.

I did use BeginInvoke for backgroundWorker1_DoWork and backgroundWorker1_RunWorkerCompleted but the problem still the same, so I post the code before I use beginInvoke. Do you have any suggestion if my code above couldn't work the way I want? I want to show a small form which has a progress bar whenever my app is downloading or uploading.

That's why I need to use backgroundWorker for the loadingForm to load. I have stucked at this point for few days already. I really appreciate your help.

Thanks in advance. – PeteMerry Dec 22 '10 at 20:44.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions