Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Backgroundworker throws error in ProgressChanged event

Status
Not open for further replies.

blairacuda

Technical User
Sep 3, 2009
51
US
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.
 
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 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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top