Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Background worker - RunWorkerCompleted code

Status
Not open for further replies.

rjoubert

Programmer
Oct 2, 2003
1,843
US
I'm using the BackgroundWorker control in VB.NET 2005 to run some file IO operations. I kickoff the BackgroundWorker using the RunWorkerAsync method, then display a dialog form with a progress bar. This all works fine. In my RunWorkerCompleted method, I close the dialog form (with the progress bar) and open another dialog form displaying a message. The first line closes the first dialog form, but that dialog form does not close until the user clicks the OK button on the 2nd dialog form. Any suggestions? Here's the code in my RunWorkerCompleted method...

Code:
dlgProgress.Close()

dlgNotification.lblMessage.Text = "BLAH BLAH BLAH"
dlgNotification.ShowDialog()

Note: dlgProgress was opened with a call to .ShowDialog
 
Have you tried opening dlgProgress with the Show() method?

IMO, using ShowDialog kind of defeats the purpose of having the work done on a separate thread, because the UI will be hung up anyway (unless the user clicks out of the dialog, at which point (s)he is no longer aware of progress) . What I normally do for things like this is temporarily show a progress bar on the form itself.

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Thanks for the response Alex, but I don't want the users to do anything else in the UI while the BackgroundWorker thread is processing. I had to use a separate thread so the progress bar would actually display. So I have to use ShowDialog.
 
Hm, interesting...

Is the ShowDialog() call to dlgProgress made before your call to RunWorkerAsync(), or from within?

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
In the event handler of a button on my form, I kick off the BackgroundWorker's process, using the call to RunWorkerAsync, then I make the call to dlgProgress.ShowDialog.
 
Aha! Since you are attempting to close the form from one thread (your BackgroundWorker's thread) and opening it on another (your main UI thread) this is probably causing the problem.

You might try opening dlgProgress from within the handler for your background worker's "DoWork" event.

Hope this helps,

Alex


[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
I tried that, but there are two issues with that...
1. The user can click around in the UI. dlgProgress still stays on top of the rest of the UI though.
2. dlgProgress never closes, even though the work is all done.
It seems that the RunWorkerCompleted event is part of the main UI thread, not part of the BackgroundWorker. I'm successfully processing other parts of the UI (refreshing controls, etc) in the RunWorkerCompleted event already.
 
You need to remember, when you use ShowDialog(), control does not return to your main form until the dialog is closed (and in this case, your RunWorkerAsync() call has completed, and your RunWorkerCompleted event has already fired).

Without seeing all your code I can't be certain, but I suspect you have quite a mess there, and aren't managing yoru two threads very well. How are you even reporting progress to a progress bar on a different form? It seems to me like you might have some very serious problems brewing here, aside from your dialog boxes not closing in order.

You might be better off putting your background worker onto your dialog form and running the workload from there, if all you want to use the background worker for is to show a progress bar.

I suggest reading this tutorial on threading before making a decision on what to do:
Without seeing all of your code I can't give very good advice on how to fix your problem. I think that with a better understanding of threading, you will be able to find the best way to fix it on your own.

Hope this helps,

Alex

[small]----signature below----[/small]
Majority rule don't work in mental institutions

My Crummy Web Page
 
Thanks again for your help Alex. I started out using the BackgroundWorker control and used the following PDF as an example. It's pretty simple actually.


I made mine even simpler by setting my Progress bar's style to Marquee. That way I didn't have to worry about reporting the progress of the BackgroundWorker's process.

At any rate, I suspected that this had nothing to do with threading. So I tried one small change. Instead of calling the Close method on dlgProgress, I called Dispose. Believe it or not, that did the trick!
 
Wow, threading simple. You are probably doing something wrong then.
Or you really don't understand threading.

threading is always hard because impossible to debug and forms in whatever form should only run on the main thread your operations can run on different threads. the forms are a different story. Don't open and close them in seperate threads, that's the best advice I can give you.

Christiaan Baes
Belgium

My Blog
 
The BackgroundWorker encapsulates a lot of the threading functionality, making it easier to implement threading in your applications. If you look at the code example in the link I posted, you'll see how simple it is. My code is even simpler than the code in that PDF article. At any rate, like I said, it wasn't even the threading that was causing my problem.
 
I certainly didn't mean to come off like that Christian...if my assumptions are wrong, please let me know. I am assuming that the RunWorkerCompleted code runs on the main UI thread. I am updating the UI in that block of code, and not receiving any errors like I was when I tried to access the UI from the RunWorkerAsync method. I am opening the progress bar dialog box from an OnClick event of a button (main UI thread) and closing it in the RunWorkerCompleted method, which I am assuming is also in the main UI thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top