Link to home
Start Free TrialLog in
Avatar of kennon2000
kennon2000

asked on

Make thing work behind ShowDialog(this)

I want to disable everything during a long calculation, so I instantate a small window to tell user to wait and show the window using :

MYForm myForm=new MYForm();
myForm.ShowDialog(this);

try{
  myLongCalculation();
 myForm.Close();
}
Catch(exc Exception)
{
  LogError(exc.ToString());
}

However, the flow is held after ShowDialog().  What is the proper way to let calculation run behind while UI are disabled by ShowDialog()?
Avatar of NetworkArchitek
NetworkArchitek

Well in there are a couple things you could do. You run the calculation in a seperate thread. If you really want to disable the UI you could do like 1: disable all UI controls, 2: run calculation in seperate thread and set it to callback when finished 3: in the call back enable all the UI controls. The other thing you could do is make the calculations happen in "myForm" and then showdialog will hold the flow until myForm comes back, if you use showdialog(). Another alternative which is a variation of the first is to disable all the controls, run the calculation, then re-enable all the controls all in the same thread. There are other possibilities too, it just depends what you want to accomplish.
You must call the method "myLongCalculation" in a new thread.
See this code:

MYForm myForm=new MYForm();
myForm.Show();//Don't call myForm.ShowDialog();
System.Threading.ThreadStart start = new System.Threading.ThreadStart( myLongCalculation );
System.Threading.Thread thread = new System.Threading.Thread(start);
thread.Priority = System.Threading.ThreadPriority.Lowest;
thread.Start();
while(!thread.Join(10))
      Application.DoEvents();
myForm.Hide();
myForm.Dispose();

Good luck
VINHNL
Avatar of kennon2000

ASKER

Can you explain why the following is needed?
while(!thread.Join(10))
     Application.DoEvents();

Thanks
ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial