×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Backgroundworker throws error in ProgressChanged event

Backgroundworker throws error in ProgressChanged event

Backgroundworker throws error in ProgressChanged event

(OP)
hello,

i am writing up my first backgroundworker and while it seems like a great tool i am running into a wall.

scenario:  we have a vb6 project that we are slowly migrating to c#.  all of our new tools/windows are planned to be written in c#.  so i have a class that is com accessible that launches a wpf form.  when the form loads i am kicking off the following code:

CODE

private void LoadUpItems()
{
  bgw = new BackgroundWorker();
  bgw.WorkerSupportsCancellation = false;
  bgw.WorkerReportsProgress = true;
  bgw.DoWork += new DoWorkEventHandler( bgw_DoWork );
  bgw.ProgressChanged += new ProgressChangedEventHandler( bgw_ProgressChanged );
  bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler( bgw_RunWorkerCompleted );
  bgw.RunWorkerAsync();
}
private void bgw_DoWork( object sender, DoWorkEventArgs e )
{
  qLib.BuildItemList( false );   
  bgw.ReportProgress( 100 );
}

private void bgw_ProgressChanged( object sender, ProgressChangedEventArgs e )
{
  progressBar1.Visibility = System.Windows.Visibility.Collapsed;
}

private void bgw_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
{
  qLib.CloseIfOpen();
}

qLib.BuildItemList is the reason for wanting have the background worker.  the list is built by reading a database and the amount of items can be huge.  progressBar1 is an Indeterminate progress bar that bounces while the list is being built and is a member of the wpf form; don't care about truly reporting progress right now.  i collapsing the progress bar in bgw_ProgressChanged because that is what i read you are supposed to update UI elements.  i've tried it there and in bgw_RunWorkerCompleted and receive the same result.  the result being:"The calling thread cannot access this object because a different thread owns it.".

in the solution containing my com accessible assembly i have a tester form with a button that calls the same code as my vb6 project.  this tester project works fine with the code above but calling the same method from vb6 results in the error

CBlair
Crystal, InstallShield, branching out in other programming realms.

RE: Backgroundworker throws error in ProgressChanged event

(OP)
i solved my issue this morning.

in my worker completed event i am doing the following:

CODE

private void bgw_RunWorkerCompleted( object sender, RunWorkerCompletedEventArgs e )
{
   CheckDelegateAndBuildList();
}

private delegate void ListBuilder();

private void CheckDelegateAndBuildList()
{
   if ( this.Dispatcher.CheckAccess() )
   {
      FinishUpLoad();
      qLib.CloseIfOpen();
   }
   else
   {
      this.Dispatcher.Invoke( System.Windows.Threading.DispatcherPriority.Normal, new ListBuilder( this.CheckDelegateAndBuildList ) );
   }
}

FinishUpLoad() is handling my UI manipulation.  i had seen this pattern used in multiple examples but instead of "Dispatcher" the examples were using "InvokeRequired" and "Invoke".  but i am using WPF so those methods are not available on their own.  instead i had to use "Dispatcher".  once i found this site http://blog.somecreativity.com/2008/01/10/wpf-equivalent-of-invokerequired/ all became clear.

i certainly don't claim to know exactly what is going on and i won't be able to go in to any kind of detail on why this works.  all i know is that a UI thread can not be reached from a worker thread unless you have a delegate to handle the communication.  i wish i could provide a more succinct answer for any who run across the same problem and need a similar solution, but i am too new at this programming stuff to completely wrap my head around multi-threading or cross threading or whatever kind of phrase needs to be used to describe what i did.  however, i would love to hear from some experts here on  a better explanation.

CBlair
Crystal, InstallShield, branching out in other programming realms.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close