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
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.
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.

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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With