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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:04:45+00:00 2026-05-18T22:04:45+00:00

One thing that’s always confused me is how a BackgroundWorker seems to have thread-safe

  • 0

One thing that’s always confused me is how a BackgroundWorker seems to have thread-safe access to the instance variables of the surrounding class.

Given a basic class:

public class BackgroundProcessor
{
    public List<int> Items { get; private set; }

    public BackgroundProcessor(IEnumerable<int> items)
    {
        Items = new List<int>(items);
    }

    public void DoWork()
    {
        BackgroundWorker worker = new BackgroundWorker();
        worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
        worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        worker.RunWorkerAsync();
    }

    void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        var processor = new ProcessingClass();

        processor.Process(this.Items); //Accessing the instance variable
    }

    void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        //Stuff goes here
    }
}

Am I erroneous in my assumption the the call to processor.Process(this.Points); is a thread-safe call? How don’t I get a cross-thread access violation?

I’m sure it’s obvious, but it always has confused me.

  • 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-18T22:04:46+00:00Added an answer on May 18, 2026 at 10:04 pm

    It’s not thread-safe, it just looks that way. You only get the cross-thread exception for GUI controls. There is a hard requirement that GUI controls only be accessed from the thread that created them. So the framework takes the time to check for calls from other threads. Note that this is actually orthogonal to synchronization issues (at least from our point of view, not from the point of view of the USER subsystem) as you could still use locks to prevent multiple threads from accessing a control at the same time and still get a cross-thread violation.

    Since the member variable is not a GUI control, there is no check for cross-thread calls, nor is there a check for race conditions. You have to do that yourself with locking or some other mechanism. You will get no exception unless the collection classes get corrupted. I don’t believe they are thread-safe, but I’m not sure what that ends up meaning, nor have I ever shared those kinds of variables between threads running at the same time, so I haven’t actually had the problem. Suffice it to say, it’s better to do the right thing than to hope that the collection classes magically work.

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

Sidebar

Related Questions

We just changed over to mercurial from subversion and there is one thing that
I need to create slideshow for gallery. And one thing that I don't know
As I write my WPF and Silverlight apps, one thing that is starting to
I am using eclipse cdt to develop a c++ program. One thing that drives
I am making a small program in C# for Windows Phone. One thing that
I have been working through some of the sync framework demos and one thing
I have a Git repository on my local machine that I cloned from my
I have a QLabel with a Qt stylesheet that sets a dark background: QLabel
We are considering development of a mission critical application in Java EE, and one
I've recently taken an existing ASP.NET 3.5 web forms application that was upgraded to

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.