Reputation: 8201
What approach would you suggest for ignoring user requests (like mouse clicks) while application is busy (meaning that UI thread is blocked doing something). Concrete example for this might be sorting of the grid control. If we say that sorting is slow, then while the operation is running I would like to ignore additional nurveous user clicks which pile up and execute the operation many times.
Is there a "fancy" way / trick of doing this in WPF, besides resorting to disabling controls, or making the long running operation async, and then implementing producer-consumer pattern manually for user request (basically ignoring any requests while operation is running)?
Upvotes: 4
Views: 2409
Reputation: 4919
One way to do this is to hook the Dispatcher OperationStarted and OperationCompleted events. The handlers to these events will set a boolean property that is bound to the IsHitTestVisible property of your window. This way when the Dispatcher is processing it will prevent the user interacting with the UI.
An example of this is as follows:
ViewModel:
private bool _appIdle = true;
private void Hooks_OperationStarted(object sender, DispatcherHookEventArgs e)
{
ApplicationIdle = false;
}
private void Hooks_OperationCompleted(object sender, DispatcherHookEventArgs e)
{
ApplicationIdle = true;
}
public bool ApplicationIdle
{
get { return _appIdle; }
set
{
_appIdle = value;
RaisePropertyChanged("ApplicationIdle");
}
}
public MainWindowViewModel()
{
Application.Current.Dispatcher.Hooks.OperationStarted += Hooks_OperationStarted;
Application.Current.Dispatcher.Hooks.OperationCompleted += Hooks_OperationCompleted;
}
MainWindow xaml:
IsHitTestVisible="{Binding ApplicationIdle}"
Upvotes: 3