I am trying to create a progress bar that will work asynchronously with the main process. I created a new event and raised it, but every time I try to perform operations on the progress bar, I get the following error:
"The calling thread cannot access this object because another thread belongs to it"
The following code is an attempt to send an instance of the execution line to the event as an object, it obviously failed, but it gives you an idea of how the code looks.
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 any ideas how to do this.
Thanks in advance SumGuy.
source
share