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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:14:13+00:00 2026-06-09T18:14:13+00:00

Suppose I have two threads, thread A and thread B. What happens in C#

  • 0

Suppose I have two threads, thread A and thread B. What happens in C# when thread A sets an object, call it object X, to a value, and thread B comes along and tries to fetch that object while it is getting set by thread A? Would C# throw an exception, would B receive object X before A makes changes to it, or would B receive object X after A makes changes to it?

I thought it was clear, but here is the code (and even with added synchronization I am still not 100% sure that this would cause the exact case I mentioned above to occur):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication8 {
    public partial class Form1 : Form {
        private AutoResetEvent waitEvent = new AutoResetEvent(false);
        int objectX = 0;

        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            DateTime startTime = DateTime.Now;
            Console.WriteLine("---------------------------- START @ " + startTime + " ----------------------------------------------------------------------");
            Thread A = new Thread(ThreadAWorkMethod);
            Thread B = new Thread(ThreadBWorkMethod);
            objectX = 0;
            waitEvent = new AutoResetEvent(false);
            A.Start();
            B.Start();
            A.Join();
            B.Join();
            Console.WriteLine("---------------------------- FINISHED AFTER " + (DateTime.Now - startTime).TotalMilliseconds + " ----------------------------");
        }

        public void ThreadAWorkMethod() {
            waitEvent.WaitOne();
            objectX = 5;
            Console.WriteLine("Value has been changed to: " + objectX + " in thread A at " + DateTime.Now);
            return;
        }

        public void ThreadBWorkMethod() {
            waitEvent.Set();
            string xInThreadB = objectX.ToString();
            Console.WriteLine("Value in thread B: " + xInThreadB + " at " + DateTime.Now);
            return;
        }
    }
}

The output of thread B to the console looks to be either 0 or 5, but even still you cannot say for certain which thread got serviced first even by checking the DateTime because both threads will have identical timestamps…and one output always makes it to the console first, so whose to say which got serviced first and if the threads actually collided on a get-set. So, in the end it seems that locking is implemented on variables in C# from a lower framework level as some people have mentioned.

  • 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-09T18:14:15+00:00Added an answer on June 9, 2026 at 6:14 pm

    Would C# throw an exception

    No, it will likely not. There is nothing in the runtime that will check for this condition automatically and raise an exception.

    However, the “value” being set may or may not be in a valid state at that point, so as soon as the result is used, whatever uses the value could easily raise an exception.

    would B receive object X before A makes changes to it, or would B receive object X after A makes changes to it?

    Either is possible. It’s also possible, depending on the type of “object”, that B would receive the object in an invalid state.

    If you know multiple threads will be accessing a value, you should always take care to synchronize the access to your property. There are many options for synchronization, from using the (relatively) simple lock statement to other more complex synchronization options (such as ReaderWriterLock/ReaderWriterLockSlim, Mutex, Semaphore, etc).

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

Sidebar

Related Questions

Suppose I have two threads. On one thread, I'm performing a sequence of jobs.
I have two threads that use an event for synchronization. In each thread they
Suppose I have two threads: A and B. When A creates an object(for example
Suppose I have two threads A and B that are both incrementing a ~global~
Suppose I have two classes that both contain a static variable, XmlTag. The second
Suppose I have a instance variable that has original value: Integer mMyInt = 1;
Suppose I have a custom collection class that provides some internal thread synchronization. For
I have two methods that I need to run, lets call them metA and
Suppose I have a function that tries to protect a global counter using this
Suppose we have a shared queue (implemented using an array), which two threads can

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.