Disable All Events on a Windows Form

后端 未结 5 796
忘了有多久
忘了有多久 2021-01-19 15:21

Is there any way to temporarily disable ALL events on an windows form?

I have situation where processing on a secondary thread is being corrupted by events on the

5条回答
  •  臣服心动
    2021-01-19 16:09

    A few different options should allow you to solve this:

    • Disable any controls that would fire events that are not thread-safe,
    • Lock all code that modifies the variables, or all event-raisers, using a Monitor on the same Object, or
    • Detach and reattach all handlers.

    I would go with the first option, possibly coupled with setting the cursor to an hourglass and/or setting a message in the status bar. It's simple, it works, and it doesn't cause the actual UI to freeze. I would avoid the third option as it will "break" the behavior of your form while it's working. The second option's efficacy is dependent on how you implement it; it could very easily freeze the UI which is something you don't want to do..

提交回复
热议问题