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()?
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()?
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.ThreadSta rt start = new System.Threading.ThreadSta rt( myLongCalculation );
System.Threading.Thread thread = new System.Threading.Thread(st art);
thread.Priority = System.Threading.ThreadPri ority.Lowe st;
thread.Start();
while(!thread.Join(10))
Application.DoEvents();
myForm.Hide();
myForm.Dispose();
Good luck
VINHNL
See this code:
MYForm myForm=new MYForm();
myForm.Show();//Don't call myForm.ShowDialog();
System.Threading.ThreadSta
System.Threading.Thread thread = new System.Threading.Thread(st
thread.Priority = System.Threading.ThreadPri
thread.Start();
while(!thread.Join(10))
Application.DoEvents();
myForm.Hide();
myForm.Dispose();
Good luck
VINHNL
ASKER
Can you explain why the following is needed?
while(!thread.Join(10))
Application.DoEvents();
Thanks
while(!thread.Join(10))
Application.DoEvents();
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.