English 中文(简体)
长期运行过程中可辨别的 mo点?
原标题:Disable mouse clicks during long-running process?
  • 时间:2011-05-16 15:51:20
  •  标签:
  • c#
  • wpf

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?

最佳回答

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

问题回答

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).





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签