Reputation: 75
I am trying to open a new child form (BuyNow Form) when a thread exits and I determined license type to be expired. During a thread, I open a new process that does the license calculation and sends the information back to the Main process. The problem is, thread gets exited before all the information is received so I can't open the BuyNow form, because it doesn't know the license is expired yet.
This is the code:
BackgroundWorker BW = new BackgroundWorker();
BW.DoWork += (sender, e) =>
{
Process.Start("LicenseManager.exe", "-Check");
// PSI Info, DataRecieved Event Args and etc.. included in Process.
// Opens the process which takes about 5 second to calculate and post
// license information. Then LicenseMode changes to TrialExpired
// if not activated and trial expired.
};
BW.RunWorkerCompleted += (sender, e) =>
{
// This never gets executed because License is not changed to TrialExpired by the time BW is completed.
if (LicenseMode == License.TrialExpired)
{
new BuyNowForm().Show();
}
};
BW.RunWorkerAsync();
I tried to open the BuyNowForm while in a thread but that freezes the newly created form. I don't want to try Thread.Sleep and wait few extra seconds for the process to send information. Thread.Join() seems to freeze the Main UI for few seconds. Process.Exited += does not work either, cuz the process was created in a thread so when it exits, BuyNowForm is still being created in a thread.
Anything you guys can think off?
Upvotes: 2
Views: 160
Reputation: 11176
The Process
class has a WaitForExit
method. This will wait, surprise, until the process exits. You have to do that (obviously) in DoWork
.
You should not open a form from another thread (which is true for DoWork
). RunWorkerCompleted
would be the correct way, because it is executed in main GUI thread.
Upvotes: 3