简体   繁体   中英

Disable mouse clicks during long-running process?

I have an issue where when a button is clicked and the process takes a while, the user clicks on other locations on the GUI screen. Those button clicks get queued up in the dispatcher so after the initial button click those other locations get clicked. I want to prevent this from happening so other UI controls don't get clicked after it's done processing.

The only solution I can think of is to p/invoke into the mouse events and prevent them from being passed to the application while the button click is being processed. Is this the best approach?

One of the easier solutions to this is to have some sort of semi-transparent overlay (potentially with progress information) that you pop up when there are long-running tasks going on. That will reliably disable the entire UI until you remove the overlay (which keeps you from having to do a lot of changing/binding IsEnabled properties).

If the process takes a while, it is generally recommended to perform such process in a worker thread, rather than in a GUI thread. So use BackgroundWorker approach like in this CodeProject article , and then use CanExecute method of commands associated with specified buttons, like here:

WPF Commanding – When do Commands re-evaluate their CanExecute method?
WPF – CanExecute refreshed

EDIT:

You can bind IsEnabled property of your other controls to the result of Command's CanExecute method.

How to disable combobox when command canExecute returns false
How to bind a ComboBoxItem's IsEnabled property to the result of a Command's CanExecute method

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM