I have 2 winforms, one (call it Form1) contains a background worker, and that form opens a second form (Form2). Form1 starts a background worker, and while its working opens Form2 so the user can do something else. When the user is done on Form2 and clicks submit, I want to check if Form1’s background worker is done and if it isn’t pop a message asking the user to wait a minute, else allow the Form2 submit.
How can I check the status of Form1’s background worker from Form2? I looked at this post Check to see if a thread in another form is still running , but my situation is different and I’m not sure how to adapt.
Check the BackgroundWorker.IsBusy property.
Edit
There are various options for making this information available to the second form.
You could pass the BackgroundWorker to Form2 on creation. Form2 can then check the
IsBusyproperty.You could create a public property
IsRunningBackgroundWorkon Form1, and give Form2 a reference to Form1. The getter for this property would checkBackgroundWorker.IsBusy.You could create a public property
CanSubmiton Form2, and set it from Form1 when the BackgroundWorker completes.bgw.RunWorkerCompleted += (s, e) => form2.CanSubmit = true;Alternatively (and I think this is best) you could put the submission code in Form1, and just use Form2 as a dialog. Then Form2 doesn’t need to know anything about Form1. Form1 opens Form2 modally, and when Form2 closes, Form1 checks if the worker is finished, and acts accordingly.