WPF Please Wait Dialog

I am developing a WPF desktop application in C # 4.0, which should handle many lengthy operations (loading data from the database, calculating simulation, optimizing routes, etc.).

When these lengthy operations are performed in the background, I want to show the Please Wait dialog. When the Pending dialog box is displayed, the application should be blocked, but simply disabling the application window is not a good idea, because all DataGrids will lose their status ( SelectedItem ).

That I still work, but there are some problems: The new WaitXUI is created using the Create-factory method. The Create method expects subtitle text and a link to the host control that should be locked. The Create method sets the StartupLocation of the window, the title text and the host to block:

 WaitXUI wait = WaitXUI.Create("Simulation running...", this); wait.ShowDialog(new Action(() => { // long running operation })); 

With the ShowDialog method overloaded, WaitXUI may be displayed. Overloading ShowDialog expects an action that completes a lengthy operation.

In ShowDialog overload, I run the action only in my thread, and then disables host control (set Opacity to 0.5 and set IsEnabled to false) and call ShowDialog of the base class.

 public bool? ShowDialog(Action action) { bool? result = true; // start a new thread to start the submitted action Thread t = new Thread(new ThreadStart(delegate() { // start the submitted action try { Dispatcher.UnhandledException += Dispatcher_UnhandledException; Dispatcher.Invoke(DispatcherPriority.Normal, action); } catch (Exception ex) { throw ex; } finally { // close the window Dispatcher.UnhandledException -= Dispatcher_UnhandledException; this.DoClose(); } })); t.Start(); if (t.ThreadState != ThreadState.Stopped) { result = this.ShowDialog(); } return result; } private new bool? ShowDialog() { DisableHost(); this.Topmost = true; return base.ShowDialog(); } private void DisableHost() { if (host != null) { host.Dispatcher.Invoke(new Action(delegate() { this.Width = host.Width - 20; host.Cursor = Cursors.Wait; host.IsEnabled = false; host.Opacity = 0.5; })); } } 

Here are the problems with this:

  • Disabling host management results due to loss of status information (SelectedItems ...)
  • WaitXUI is sometimes displayed in just a few milliseconds when the stream ends a few ms after WaitXUI is displayed.
  • Sometimes the dialog does not appear at all, although the thread is still working

These are the main problems that come to my mind. How can this concept be improved or what other methods can be used to solve this problem?

Thanks in advance!

+6
source share
2 answers

A little lateral thinking always helps when developing WPF applications. You can easily fulfill your requirements with just the Grid , Rectangle , the bool property (which you might already have) and the BooleanToVisibilityConverter and you will not need to disable any controls.

The idea is simple. Add a white Rectangle in front of the view content by setting the Opacity property from 0.5 to 0.75 . The data binds its Visibility property to the bool property in your view model or code and BooleanToVisibilityConverter :

 <Grid> <Grid> <!--Put your main content here--> </Grid> <Rectangle Fill="White" Opacity="0.7" Visibility="{Binding IsWaiting, Converter={StaticResource BooleanToVisibilityConverter}}" /> <!--You could add a 'Please Wait' TextBlock here--> </Grid> 

Now, when you want to disable controls, you simply set the bool property to true and the Rectangle will make the user interface look faded:

 IsWaiting = true; 
+8
source

No need to create your own implementation, I think that it is redundant.

Take a look at an already created component, such as BusyIndicator , for similar needs. which is vital and effective ..

Additional Info from codeplex

+1
source