Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable form while BackgroundWorker is busy?

I don't want the user to interact with my application while a certain backgroundworker is busy (working). I created this bgw so that the application doesn't look frozen when it's working. But now users can move around in the app. How do I disable my form/application?

Thanks!

like image 507
Chris Avatar asked Sep 15 '09 17:09

Chris


3 Answers

Perhaps set Enabled to false?

this.Enabled = false;

That is a bit drastic though, since it also prevents the form from being moved, resized or event closed. A better approach would be to put a Panel control in your form, that has Dock = Fill, and put our other controls into that Panel. Then you can instead use the Enabled property of the Panel to prevent further user input.

Note thought that the user will be able to close the form, so that should be handled gracefully in some way.

like image 116
Fredrik Mörk Avatar answered Sep 28 '22 07:09

Fredrik Mörk


You could also create a modal "busy splash" dialog that is shown when the background task starts and removed programmatically when the task ends. You could put a little animation in that box too to let the users know that something is going on. You would also need to make sure they don't close the dialog manually (only allow it to be closed programmatically).

like image 44
Garo Yeriazarian Avatar answered Sep 28 '22 07:09

Garo Yeriazarian


I agree with @michael-todd in his comment above, disabling individual controls/navigation buttons/etc. while the background worker is working seems like a fairly good solution. This would give you fine-grained control over what the user can do during the specific background operation.

You could:

  1. Create methods to enable or disable all appropriate navigation/interaction controls (by setting their Enabled property to true/false, etc. as appropriate for each control)
  2. Call the "Disable" method from your code right before you call StartWorkerAsync() on your background worker
  3. In the event handler for RunWorkerCompleted, call the "Enable" method to re-enable the view for user input
like image 29
Guy Starbuck Avatar answered Sep 28 '22 05:09

Guy Starbuck