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!