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 3679024
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:24:38+00:00 2026-05-19T03:24:38+00:00

I have the following code, in which I’m trying to process a large amount

  • 0

I have the following code, in which I’m trying to process a large amount of data, and update the UI. I’ve tried the same thing using a background worker, but I get a similar issue. The problem seems to be that I’m trying to use a class that was not instantiated on the new thread (the actual error is that the current thread doesn’t “own” the instance). My question is, is there a way that I can pass this instance between threads to avoid this error?

DataInterfaceClass dataInterfaceClass = new DataInterfaceClass();

private void OutputData(List<MyResult> Data)
{            
    progressBar1.Maximum = Data.Count;
    progressBar1.Minimum = 1;
    progressBar1.Value = 1;

    foreach (MyResult res in Data)
    {                       
        // Add data to listview
        UpdateStatus("Processing", res.Name);

        foreach (KeyValuePair<int, string> dets in res.Details)
        {
            ThreadPool.QueueUserWorkItem((o) =>
            {
                // Get large amount of data from DB based on key
                // – gives error because DataInterfaceClass was
                // created in different thread.
                MyResult tmpResult = dataInterfaceClass
                    .GetInfo(dets.DataKey);

                if (tmpResult == null)
                {
                    // Updates listview
                    UpdateStatus("Could not get details", 
                        dets.DataKey); 
                }
                else
                {
                    UpdateStatus("Got Details", dets.DataKey);
                }

                progressBar1.Dispatcher.BeginInvoke(
                    (Action)(() => progressBar1.Value++));
            });
        }
    }
}

EDIT:

DataInterfaceClass is actually definated and created outside of the function that it is used in, but it is an instance and not static.

  • 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-05-19T03:24:38+00:00Added an answer on May 19, 2026 at 3:24 am

    UPDATE:
    You seem to have modified the posted source code, so…

    You should create an instance of the DataInterfaceClass exclusively for each background thread or task. Provide your task with enough input to create its own instance.

    That being said, if you try to access data in a single database in a highly parallel way, this might result in database timeouts. Even if you can get your data access to work in a multithreaded way, I would recommend limiting the number of simultaneous background tasks to prevent this from occurring.

    You could use a Semaphore (or similar) to ensure that no more than a certain amount of tasks are running at the same time.

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

Sidebar

Related Questions

No related questions found

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.