Click here to Skip to main content
15,917,060 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a c# winform app built on .NET 2.0. In a nutshell, the app takes a bunch of user input in a form and runs some business logic and creates 3 output files that are saved on the user's pc.

Once information is input and the user clicks the "go" button, a new form is opened telling the user to "please wait while the app is working." Behind the scenes: once the "please wait" form is loaded, it triggers the background worker to create the files. The RunWorkerCompleted tells the "please wait" form to close and notify the user that it is complete.

The problem: If I run the application and keep focus on the app, everything works well. However, if I run the app, then switch focus to another app (i.e. look at my email) then switch back to my application, it stays frozen on the "please wait" screen.

Hopefully I described this well enough... please let me know what you think might be going on or if you think my design might be flawed. Thanks in advance!
Posted
Comments
AspDotNetDev 13-Jan-11 16:32pm    
I recommend you create a very simple example of this (as simple as you can possibly make it) and paste the code in your question. Otherwise, it's very difficult to guess what might be wrong.
atlquaker 19-Jan-11 13:13pm    
private void backWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Invoke((MethodInvoker)delegate{ProcessComplete();});
}

private void ProcessComplete()
{
this.Close();
ProcessCompleteForm pf = new ProcessCompleteForm();
pf.Show();
}
aidin Tajadod 13-Jan-11 19:48pm    
What is your "Please wait!" form? Is it a modal form? Have you specified the owner form?
atlquaker 26-Jan-11 11:53am    
Main.cs is the main core form of the application. When the user clicks the Process button, I give the user some output options in a new form:
Options optionsForm = new Options;
optionsForm.ShowDialog();

On the options form, the user selects different processing options then clicks a "Go" button which displays the Processing form:

PleaseWait pleasewaitForm = new PleaseWait();
pleasewaitForm.ShowDialog();

On processingForm load, I start the background worker. On background worker complete, I want to close the please wait form and display a "Work complete" form. I know the background worker is doing its job but it is never closing the Please Wait form. Let me know if you need more details, do you have any ideas?

1 solution

Are you using Control.Invoke[^] to communicate from the worker thread to the GUI thread? If you're not that might be your problem.

Regards
Espen Harlinn
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 13-Jan-11 20:38pm    
Espen, it's a 5. This is very likely.
One note: checking Control.InvokeRequired is useful if sometimes called from UI thread and sometimes not.
atlquaker 13-Jan-11 21:58pm    
Espen,
No, I am not, but that explanation makes a lot of sense. I thought that by using the background worker events that this was handled for me... but I guess that was a bad assumption. I am surprised it didn't throw an error if that is the case. I'll implement this on all of the background worker events that interact with the gui thread and let you know if it works. I appreciate the advice, I feel silly for not thinking of this. Thanks.
Espen Harlinn 14-Jan-11 3:05am    
Thanks SAKryukov!

atlquaker: I really hope it solves your problem, happy coding!
atlquaker 18-Jan-11 9:26am    
Well, unfortunately, it did not solve the problem... but maybe I didn't implement it correctly. This is where I stand: My background worker successfully completes (I confirmed this in debugging.) The background worker complete calls my ProcessComplete(). Within ProcessComplete() is:
this.BeginInvoke(new MethodInvoker(delegate(){ this.Close(); });
Espen Harlinn 18-Jan-11 9:35am    
use (MethodInvoker)delegate ... not new MethodInvoker(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900