[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 386
  • Last Modified:

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()?
0
kennon2000
Asked:
kennon2000
1 Solution
 
NetworkArchitekCommented:
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.
0
 
vinhnlCommented:
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
0
 
kennon2000Author Commented:
Can you explain why the following is needed?
while(!thread.Join(10))
     Application.DoEvents();

Thanks
0
 
DarthModCommented:
Submitted to PAQ with points refunded (100)

DarthMod
Community Support Moderator
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now