Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF wait window

When i want to run Long Operation i want to show model dialog window (Wait Window)

I want to display this Wait Window in different Thread as ShowDialog()

Before entering into Long Running Operation, i will display Model Dialog Window

After done with Long Running Operation, i will close this Model Dialog Window

I know instead of creating Window in another Thread, we can simply move long Running Code in another Thread but impact of this in my code is heavy.

Please suggest me a solution for this

like image 252
MMP Avatar asked Dec 05 '22 20:12

MMP


2 Answers

I would suggest you to avoid such WinForms-smell solutions like ShowDialog() calls. Take a look at the out of the box WPF ToolKit BusyIndicator it alows leverage a power and flexibility of WPF and XAML.

BusyIndicator makes it easy to let the user know when an application is busy. Simply wrap the relevant content in an instance of the BusyIndicator control and toggle its IsBusy property to True during any long-running process.

It allows build custom wait windows, for instance:

enter image description here

You have full control of UI layout and can define your own DataTemplate:

<extToolkit:BusyIndicator IsBusy="True" DisplayAfter="0">
       <extToolkit:BusyIndicator.BusyContentTemplate>
            <DataTemplate>
               ....

See Extended WPF Toolkit BusyIndicator for more examples and download.

like image 77
sll Avatar answered Dec 07 '22 23:12

sll


First of all, my advice is to remove the 'long running operation' from the UI thread.

That being said, here's an article discussing what you try to do.

http://eprystupa.wordpress.com/2008/07/28/running-wpf-application-with-multiple-ui-threads/

The following code is presented:

Thread thread = new Thread(() =>
    {
      Window1 w = new Window1();
      w.Show();

      w.Closed += (sender2, e2) =>
      w.Dispatcher.InvokeShutdown();

      System.Windows.Threading.Dispatcher.Run();
    });

thread.SetApartmentState(ApartmentState.STA);
thread.Start();

You create a new Dispatcher that will handle UI messages and events on the new thread. The attached Closed event handler ensures that the created dispatcher does not continue running after you close your form, thus keeping the application from running 'forever'.

like image 25
havardhu Avatar answered Dec 08 '22 01:12

havardhu