WPF: Asynchronous progress bar
Posted
by SumGuy
on Stack Overflow
See other posts from Stack Overflow
or by SumGuy
Published on 2009-07-20T10:18:45Z
Indexed on
2010/04/04
0:23 UTC
Read the original article
Hit count: 1322
I'm trying to create a progress bar that will work asynchronously to the main process. I'm created a new event and invoked it however everytime I then try to perform operations on the progress bar I recieve the following error:
"The calling thread cannot access this object because a different thread owns it"
The following code is an attempt to send an instance of the progress bar to the event as an object, it obviously failed but it gives you an idea of what the code looks like.
private event EventHandler importing;
void MdbDataImport_importing(object sender, EventArgs e)
{
ProgressBar pb = (ProgressBar)sender;
while (true)
{
if (pb.Value >= 200)
pb.Value = 0;
pb.Value += 10;
}
}
private void btnImport_Click(object sender, RoutedEventArgs e)
{
importing += new EventHandler(MdbDataImport_importing);
IAsyncResult aResult = null;
aResult = importing.BeginInvoke(pbDataImport, null, null, null);
importing.EndInvoke(aResult);
}
Does anyone have ideas of how to do this.
Thanks in advance SumGuy.
© Stack Overflow or respective owner