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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:07:42+00:00 2026-06-04T07:07:42+00:00

I have a sample application and wonder if anyone can shine a light on

  • 0

I have a sample application and wonder if anyone can shine a light on this. When I put a breakpoint on the for loop an step into to the code in the debugger, why does it switch from one thread to another? it does so when running the application at runtime…Please see code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TPLSample
{
    class Program
    {

        static void Main(string[] args)
        {
           Management m = new Management();

           Task a = new Task(() => m.Operation1());
           a.Start();
           Task b = new Task(() => m.Operation2());
           b.Start();


            Console.ReadLine();
        }
    }
    public class Management
    {
        A a = null;
        B b = null;

        public void Operation1()
        {
            a = new A();

        }
        public void Operation2()
        {
            b = new B();
        }
    }
    public class A
    {  Client a = new Client();
        public A()
        {
            while (true)
            {
                a.Test("Im AAAAA");
            }
        }
    }
    public class B
    {
        Client a = new Client();
        public B()
        {
            while (true)
            {
                a.Test("Im BBBBB");
            }
        }
    }
    public class Client
    {
        Object ibj = new Object();


        public void Test(string item)
        {
            lock (ibj)
            {
                for (int i = 0; i < 200000; i++)
                {
                    Console.WriteLine(item);
                }
            }

        }
    }
}

The result for this is a mixture of As and Bs. Isnt the lock suppose to block the thread so that the result will end up in sequence? The same is happening in an application I am writing except that the WHILE loops run forever (each task needs to poll continuosly). Please note that I am starting two tasks because I dont want one WHILE loop to prevent the other from running if the WHILE loop is to run forever. How do I make them go in sequence to that function in CLIENT class?

  • 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-04T07:07:43+00:00Added an answer on June 4, 2026 at 7:07 am

    This happens because if you trace your object creation tree carefully you will notice that each thread is using a different lock object, thus rendering your lock statement useless.

    Your object creation happens like this:

                     new A() -> new Client() -> new Object()
                    / 
    new Management()
                    \
                     new B() -> new Client() -> new Object()
    

    You are using the last two objects on the right as locks, and you can clearly see that they are different objects.

    Try to make the lock static:

    public class Client
    {
        static Object ibj = new Object();
        ...
    

    or otherwise rethink your hierarchy to pass the same lock to both Tasks.

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

Sidebar

Related Questions

Does anyone have a sample application which makes use of mapping models in particular
Visual Studio 2005 C++ Windows XP Pro I have a sample application that I
I have seen the sample application of iPhone MP-movie player - controller. They have
I have made a sample application which constructs a filter graph to capture audio
I have written a sample application to write to a public and private queues
I have the sample console application which is supposed to be executed in the
Does MS have a sample enterprise application that demonstrates the use of different Enterprise
I am new to Android. I have tried an sample application for status bar
I have been developing a sample application after reading Rails 3 Tutorial book. In
I have an Web-application in which I capture Image using EdgeCamShots Sample Application Saving

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.