I created a little tool for importing and converting data from one database to another. Since this takes a lot of time, I wanted to display some kind of progress for the user with a indeterminate progressbar. As long as the import is running, the progressbar should be running too. I used this site as reference : The ProgressBar control but I got trouble understanding the role of the DoWork-Method since the option for a indeterminate progressbar is set once.
I came up with a solution that works halfway, I can't stop the animation after the import's done. Could someone help me out?
XAML:
<ProgressBar Minimum="{Binding Path=Minimum}" Maximum="100" IsIndeterminate="{Binding Path=IsIndeterminate}" Height="20" Grid.Column="2" Grid.Row="5" Grid.ColumnSpan="2"/>
ViewModel:
public void StartImportThread()
{
ButtonsEnabled = false;
var th = new Thread(new ThreadStart(StartImport));
th.IsBackground = true;
th.Start();
}
public void StartImport()
{
_threadDispatcher = Dispatcher.CurrentDispatcher;
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted += worker_Complete;
worker.RunWorkerAsync();
// Doing Import Stuff
worker.CancelAsync(); // Import finished
ButtonsEnabled=true;
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// while(true)
// {
Minimum = 0;
IsIndeterminate = true;
// }
}
private void worker_Complete(object sender, RunWorkerCompletedEventArgs e)
{
// if (e.Cancelled)
// {
IsIndeterminate = false;
Minimum = 100;
// }
}