Disable all events on a Windows form

Is there a way to temporarily disable ALL window-shaped events?

I have a situation where processing on the secondary thread is corrupted by events on the main thread. (Events in the main thread change the contents of the data-bound controls to the variables used by the secondary stream.) You are looking for a way to “lock” the form until processing on the secondary stream is complete. (Obviously switching the processing to the main thread would do this, but it also “froze” the user interface, and this is not what I want.)

+4
source share
5 answers

Thanks everyone for the great help. In fact, it helped the decision-making process. Reproaches everyone. Finished with a lock. (Ie the if statement at the beginning of each event handler checks the status of the lock before continuing.) Thin and not everything that is “cool” technically, but just worked.

+1
source

Several different options should allow you to solve this problem:

  • Disable any controls that will trigger events that are not thread safe,
  • Block all code that modifies variables or all event collectors using a monitor on the same object or
  • Disconnect and reconnect all handlers.

I would go with the first option, possibly related to setting the cursor on the hourglass and / or setting the message in the status bar. It's simple, it works, and it does not freeze the actual user interface. I would avoid the third option, as it “broke” the behavior of your form during work. The second option for effectiveness depends on how you implement it; it can very easily freeze a user interface that you don't want to do.

+2
source

This is a standard thread problem. Even if you can block events, it’s very difficult to do, it still won’t solve the problem. Since the event handler can be started, but not yet finished, by the time you want to start the lock.

And it has a standard solution, use the lock keyword.

If this is impractical because the user needs to change several controls before you can run the query again, you need to complete the workflow first. For example, BackgroundWorker.CancelAsync. If this is impractical because it takes too much time to cancel the request, you need to set the Enabled property to false while the request is running.

+2
source

Blocking events on the form effectively freezes the user interface. Events occur as part of the standard message processing in the message pump. Stop events stop the user interface.

It’s better to disable the user interface features during the processing of your work (for example, disabling individual buttons, etc.), and then re-enable them when they are completed. This will allow normal events to occur, but will not allow the user to trigger a button click, etc.

+1
source

If you want to disable all events as a result of PostMessage , you can implement IMessageFilter and use AddMessageFilter .

This will probably mask all the events you are interested in. It will not catch everything, for example an event raised by SendMessage . To completely capture everything, you will need to bind the application to specific applications .

Please note that this will cause the user interface to completely freeze, which may not be what you want. The best helper might be to intercept related events and suppress unwanted behavior while your background job is doing.

+1
source

All Articles