Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8420563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:54:41+00:00 2026-06-10T02:54:41+00:00

I have the following set up: BackgroundWorker backgroundInstancesWorker = new BackgroundWorker(); backgroundInstancesWorker.DoWork += new

  • 0

I have the following set up:

BackgroundWorker backgroundInstancesWorker = new BackgroundWorker();
backgroundInstancesWorker.DoWork += new DoWorkEventHandler(EnumerateInstances);
backgroundInstancesWorker.WorkerReportsProgress = false;
backgroundInstancesWorker.WorkerSupportsCancellation = false;
backgroundInstancesWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundInstancesWorker_RunWorkerCompleted);

// Create temporary tuple to hold the argument information
// X is type IEnumerable<Foo>, Y is type Bar
object arguments = new object[2] { X, Y };
backgroundInstancesWorker.RunWorkerAsync(arguments);

Thread worker function:

private static void EnumerateInstances(object sender, DoWorkEventArgs e)
{
     object[] arguments = e.Argument as object[];
     var queryCounterSets = arguments[0] as IEnumerable<Foo>;
     var sourceItem = arguments[1] as Bar;
     e.Result = sourceItem;
}

Finally completed function:

private static void backgroundInstancesWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
      if (e.Result != null && e.Result is Bar)
      {
          // Do stuff
      }
}

However, in the RunWorkerCompleted function, when I try to access e.Result object, it gives me TargetInvocationException and saying that the calling thread does not have access to the object because another thread owns it. Anyone has any insights as to why this is a problem? I simply want to pass the Bar object into RunWorkerCompleted once the background thread finishes.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:54:42+00:00Added an answer on June 10, 2026 at 2:54 am

    Your RunWorkerCompleted event handler should always check the AsyncCompletedEventArgs.Error and AsyncCompletedEventArgs.Cancelled properties before accessing the RunWorkerCompletedEventArgs.Result property. If an exception was raised or if the operation was canceled, accessing the RunWorkerCompletedEventArgs.Result property raises an exception.

    The error probably occurred in your background function. See this question for some information on handling cross-thread access of thread-affine objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following set of code: using (OracleConnection conn = new OracleConnection(m_fceConn)) {
I have the following set of data: ID | CREATED | USER -------------------------- 1
I'm trying to map a Dictionary containing Lists. I have the following set of
I'm using log4Net for my logging. I also have the following set... <log4net debug=true>
I have the following function set up to sequentially fade in divs (with class
I have the following query: SET @q = 12; UPDATE `table` SET qty =
I have the following animation set up using Three.js: http://frontier.lincoln.ac.uk/3d/development/Stage2/characters/man2.html Please ignore the ropey
I have the following data set structure: date time_in_hours price Sep 03 08 9.76
I have the following relationships set up: A HABTM B B belongsTo C C
I have the following code set up in my Startup IDictionary<string, string> properties =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.