I can’t get my progress bar to work. Any help is much appreciated!
Here’s the code:
<Window x:Class="BulkSAConfigureControl.BulkSaProgressBar"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Please Wait.." Height="60" Width="300" WindowStyle="ToolWindow" ResizeMode="NoResize" WindowStartupLocation="CenterScreen">
<ProgressBar Name="progressBar" IsIndeterminate="True">
<ProgressBar.Resources>
<ResourceDictionary Source="/PresentationFramework.Aero;v3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" />
</ProgressBar.Resources>
</ProgressBar>
.
public class ProgressBarClass : Window
{
public ProgressBarClass()
{
InitializeComponent();
}
public void StartProgressBar()
{
Duration d = new Duration(TimeSpan.FromSeconds(5));
DoubleAnimation anim = new DoubleAnimation(100.0, d);
progressBar.BeginAnimation(ProgressBar.ValueProperty, anim);
this.Show();
}
public void StopProgressBar()
{
this.Close();
}
}
.
public class DoSomething : UserControl
{
public void DoSomeStuff()
{
ProgressBarClass pBar = new ProgressBarClass();
pBar.StartProgressBar();
// Do some stuff here
pBar.StopProgressBar();
}
}
To add what Mark says, there are two ways to fix the problem. The hack way and the proper way.
The proper way is to use threading, such as a BackgroundWorker. You will likely end up using control.BeginInvoke to handle updating the GUI thread.
The hack way is to call Application.DoEvents in your loop. I call this the hack because it only partially works. For example, if you’re doing a tight loop that has lots of little quick instructions then it will work fine’ish. If you’re doing a loop in which your instructions take a while, this will not work (such as making a huge sql query to a database).
Here is a good link to learn about this particular example. I think WPF handles things only slightly different than regular WinForms.