Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting Screen for WPF window Loading

I am navigating from some window window1 to Mainwindow

In my MainWindow_loaded Method i have too much computation so when i navigate to main window windows goes white untill all computation finished and window loaded

I tried in MainWindow

 private void MainWindow_Loaded_1(object sender, RoutedEventArgs e)

 BackgroundWorker worker = new BackgroundWorker();

       worker.DoWork += (o, ea) =>
        {
             do large computation
          };

          worker.RunWorkerCompleted += (o, ea) =>
           {

               _busy.IsBusy = false;
          };
         _busy.IsBusy = true;
         worker.RunWorkerAsync();

}

But problem is that it navigates to Mainwindow without computation of necessary data in loaded event handler and doesnt even show waiting bar in UI ? ? Is it possible to show waiting bar and all computation of data in MainWindow_Loaded?

I have used ExtendedWpfToolkit for _busy which is busy indicator

like image 685
uncia Avatar asked Feb 17 '23 13:02

uncia


2 Answers

You can subscribe your background worker to report progress.

worker.WorkerReportsProgress = true;

And now you can have this progress report be triggered by an event you subscribe to.

worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

In doing so, you can create a progress bar than can update itself based on this worker_ProgressChanged event, triggered by your computation.

It appears you've already figured out IsBusy, so you can have your progress bar show only when this is true.

like image 65
tnw Avatar answered Feb 20 '23 12:02

tnw


You can use BusyIndicator control. It is part of the Extended WPF ToolKit.

I have created a sample app using it. Below is screen shot of the app which displays the loop count.

Waiting Screen

Here is a tutorial on how to use it.

Sample code.

Note: You need to download the tool kit and add a reference to Xceed.Wpf.Toolkit.dll in your project.

XAML code:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WPFTool="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
        Title="MainWindow" Height="200" Width="300" Loaded="Window_Loaded">
    <WPFTool:BusyIndicator Name="BusyIndicator">
        <Grid>
        </Grid>
    </WPFTool:BusyIndicator>
</Window>

CodeBehind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            BusyIndicator.IsBusy = true;
            BusyIndicator.BusyContent = "Initializing...";
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (o, a) =>
                {
                    for (int index = 0; index < 5; index++)
                    {
                        Dispatcher.Invoke(new Action(() =>
                        {
                            BusyIndicator.BusyContent = "Loop : " + index;
                        }), null);
                        Thread.Sleep(new TimeSpan(0, 0, 1));
                    }
                };
            worker.RunWorkerCompleted += (o, a) =>
                {
                    BusyIndicator.IsBusy = false;
                };
            worker.RunWorkerAsync();
        }

    }
}
like image 29
Anand Murali Avatar answered Feb 20 '23 11:02

Anand Murali