Issue with opening multiple form using delegate.BeginInvoke()...new form get hanged...Plz help
-
Hi All, I am bogged down with some weird issue. I have one login form having login button. At the click of login button, I need to show one form (say form2). This form2 do some buzy operation (5 second) in form2_load event. I need to show one progress bar also (at click of login button - before showing form2) telling user that something is running - so pls wait... Coding stuffs: I am using delegate's BeginInvoke code. Below is login button click event:
BuzyOperationDelegate del = new BuzyOperationDelegate(BuzyOperation); IAsyncResult result = del.BeginInvoke(null,null); while (result.IsCompleted == false) { //progress bar shown here... }
In BuzyOperation(), I am opening new form (i.e. form2) as shown below:
private int BuzyOperation() { Form2 frm2 = new Form2(); frm2.Show();//this will take 5sec. return 1; }
Here is my issue. form2 get hanged. :zzz: Progress bar is running absolutely correct asynchronously :thumbsup:. I am also using Application.DoEvents(). But still form2 gets hanged. :zzz: Plz help... Chintan
Thanks in advance, Chintan.
-
Hi All, I am bogged down with some weird issue. I have one login form having login button. At the click of login button, I need to show one form (say form2). This form2 do some buzy operation (5 second) in form2_load event. I need to show one progress bar also (at click of login button - before showing form2) telling user that something is running - so pls wait... Coding stuffs: I am using delegate's BeginInvoke code. Below is login button click event:
BuzyOperationDelegate del = new BuzyOperationDelegate(BuzyOperation); IAsyncResult result = del.BeginInvoke(null,null); while (result.IsCompleted == false) { //progress bar shown here... }
In BuzyOperation(), I am opening new form (i.e. form2) as shown below:
private int BuzyOperation() { Form2 frm2 = new Form2(); frm2.Show();//this will take 5sec. return 1; }
Here is my issue. form2 get hanged. :zzz: Progress bar is running absolutely correct asynchronously :thumbsup:. I am also using Application.DoEvents(). But still form2 gets hanged. :zzz: Plz help... Chintan
Thanks in advance, Chintan.
Try to avoid Application.DoEvents... It's the wrong way to do things in .NET. What you want is a BackgroundWorker. That's designed specifically for this situation. It goes something like this... 1) Create a BackgroundWorker for the "BusyOperation" 2) Create your progress dialog, and pass the worker to it, either in the constructor or via a property 3) The dialog hooks the RunWorkerCompleted event of the worker (Triggers when it's done), and optionally the progress changed event (See the MSDN documentation) 4) The dialog runs the background worker (Do not sit in a loop - Just exit the method). "BusyOperation" will automatically start asynchronously in a background thread. 5) When the RunWorkerCompleted event triggers, the dialog closes and returns control to the main form.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Hi All, I am bogged down with some weird issue. I have one login form having login button. At the click of login button, I need to show one form (say form2). This form2 do some buzy operation (5 second) in form2_load event. I need to show one progress bar also (at click of login button - before showing form2) telling user that something is running - so pls wait... Coding stuffs: I am using delegate's BeginInvoke code. Below is login button click event:
BuzyOperationDelegate del = new BuzyOperationDelegate(BuzyOperation); IAsyncResult result = del.BeginInvoke(null,null); while (result.IsCompleted == false) { //progress bar shown here... }
In BuzyOperation(), I am opening new form (i.e. form2) as shown below:
private int BuzyOperation() { Form2 frm2 = new Form2(); frm2.Show();//this will take 5sec. return 1; }
Here is my issue. form2 get hanged. :zzz: Progress bar is running absolutely correct asynchronously :thumbsup:. I am also using Application.DoEvents(). But still form2 gets hanged. :zzz: Plz help... Chintan
Thanks in advance, Chintan.
Check out this tip/trick: http://www.codeproject.com/Tips/53695/Multiple-Subsequent-Main-Forms-in-Csharp-Apps.aspx[^] I think it will help you do what you want to do.
.45 ACP - because shooting twice is just silly
-----
"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." - J. Jystad, 2001 -
Try to avoid Application.DoEvents... It's the wrong way to do things in .NET. What you want is a BackgroundWorker. That's designed specifically for this situation. It goes something like this... 1) Create a BackgroundWorker for the "BusyOperation" 2) Create your progress dialog, and pass the worker to it, either in the constructor or via a property 3) The dialog hooks the RunWorkerCompleted event of the worker (Triggers when it's done), and optionally the progress changed event (See the MSDN documentation) 4) The dialog runs the background worker (Do not sit in a loop - Just exit the method). "BusyOperation" will automatically start asynchronously in a background thread. 5) When the RunWorkerCompleted event triggers, the dialog closes and returns control to the main form.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Hi Ian, Thanks for your great advice. I tried BackGroundWorker component also. However, I am getting same result - Form2 get hanged. Thereafter, I tried some stupid stuff and it worked. I only copied form2_load code in public method (BuzyOperation) and called BuzyOperation() replacing it with frm2.show()
Form2 frm2; private void backgroundWorker1\_DoWork(object sender, DoWorkEventArgs e) { frm2 = new Form2(); frm2.BuzyOperation(); //Earlier it was frm2.Show(). }
After completing BuzyOperation(), below code will execute which will only show form2 with Job Done msg.
private void backgroundWorker1\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { frm2.Show();//this will call frm2\_load event, which is now empty. I copied all code to BuzyOperation() MessageBox.Show("Job done!"); }
I found the result as success, Now form2 is not getting hanged :thumbsup:. I don't know what is going inside frm2.Show() (in our earlier scenerio), but i would like to know from you. :confused: Pls. let me know.
Thanks in advance, Chintan.
-
Hi Ian, Thanks for your great advice. I tried BackGroundWorker component also. However, I am getting same result - Form2 get hanged. Thereafter, I tried some stupid stuff and it worked. I only copied form2_load code in public method (BuzyOperation) and called BuzyOperation() replacing it with frm2.show()
Form2 frm2; private void backgroundWorker1\_DoWork(object sender, DoWorkEventArgs e) { frm2 = new Form2(); frm2.BuzyOperation(); //Earlier it was frm2.Show(). }
After completing BuzyOperation(), below code will execute which will only show form2 with Job Done msg.
private void backgroundWorker1\_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { frm2.Show();//this will call frm2\_load event, which is now empty. I copied all code to BuzyOperation() MessageBox.Show("Job done!"); }
I found the result as success, Now form2 is not getting hanged :thumbsup:. I don't know what is going inside frm2.Show() (in our earlier scenerio), but i would like to know from you. :confused: Pls. let me know.
Thanks in advance, Chintan.
You're a little confused here... Don't worry, the threading is a little hard at first. There are two things to consider here... 1) The GUI thread has to stay unblocked, or your progress bar won't update 2) All controls HAVE to be created on the GUI thread, and only modified on that thread (Otherwise you get those crashes). You're having problems because you're accessing the form inside the background worker... Can't do that. The point of the worker is that it works completely independently of the GUI, just letting you know when to update the progress. So try creating the background worker from inside the Form2 constructor, then just call Form2's ShowDialog() from the main form.
public Form2()
{
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += BusyOperation;
bw.RunWorkerAsync();
}Or something like that.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)