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

  • Home
  • SEARCH
  • 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 8541423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:49:42+00:00 2026-06-11T11:49:42+00:00

I have the following code: var x = new Thread(new ThreadStart(Delegate)); x.Start(); This will

  • 0

I have the following code:

var x = new Thread(new ThreadStart(Delegate));
x.Start();

This will create a new thread and start it.

How can I detect that thread X has started to execute without a do while loop right after?

  • 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-11T11:49:43+00:00Added an answer on June 11, 2026 at 11:49 am

    Use a semaphore a mutex, or an Auto/ManualResetEvent.

    Code

    //Initialize semaphore, set it to BLOCK
    ManualResetEvent sema = new ManualResetEvent(false);
    
    void Main()
    {
        var x = new Thread(Delegate);
        //Request the system to start the thread.
        //This doesn't mean the CPU will immediately run Delegate method
        //but eventually it will do
        x.Start(sema);
    
        //Stop here and don't do anything on this thread until the semaphore is FREE
        sema.WaitOne();
    
        [continued main thread]
    }
    
    void Delegate(Semaphore sema){
        //Unblock the semaphore
        sema.Set(1);
        [your code here]
    }
    

    Deep explanation

    One of the principles behind multithreading is non-determinism. If you don’t use proper techniques, as described above, you cannot predict the behaviour of operations done in multiple threads If you have a method like this

    void Main()
    {
        A();
        B();
        C();
    }
    

    Then you are sure that B is never executed before A or after C. The same doesn’t apply to multithreading.

    void Main()
    {
        new Thread(A).Start();
        new Thread(B).Start();
        new Thread(C).Start();
    
        D();
    }
    

    You are sure that the thread running B is started after the thread running A, but in multithreading this means something different. As of MSDN and every programming book, starting a thread merely means requesting the OS to allocate proper facilities in kernel to support multithreading. If this is done (the thread is correctly created and scheduled for execution) then the method returns without error. It can happen that the OS runs the three threads in any order, depending on several factors.

    So if you debug them to console (think each does a Console.WriteLine("Hello, I'm thread A/B/C"), you can get any order in different executions: A,B,C;A,C,B;B,C,A and so on.

    So you now want to make sure, but really, really sure, that a particular or every thread has really started before running D. In fact, in many of the single-core CPU cases, the OS is supposed to run D method before every thread. That’s unpredictable too! So after being unable to predict when A, B and C run, you cannot predict when D runs!!

    Explicit synchronization is the technique to forcefully pause the execution of code and wait for an event to occur. The event depicted by the release of the semaphore depends on the context, so in your case, you’re just telling the main thread “Wait for Delegate to have started, then do whatever you want” 🙂

    Alternate, inefficient method

    Using semaphores is just an efficient way of doing the following with an infinite loop

    volatile bool threadStarted = false;
    
    void Main()
    {
        var x = new Thread(Delegate);
        x.Start();
    
        while (!threadStarted);
        [continued main thread]
    }
    
    void Delegate(Semaphore sema){
        threadStarted = true;
        [your code here]
    }
    

    Using semaphore doesn’t simply waste CPU for continuously checking if a certain flag is low or high

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

Sidebar

Related Questions

So I have the following code block: var sw = new Stopwatch(); sw.Start(); while
I have the following code: var carsContext = new CarsDataContext(); IQueryable<Car> allCars = carsContext.Cars;
I have the following code: var tlp = new TableLayoutPanel { Location = new
I have the following code for mongoose schemas var EstacionSchema = new Schema({ nombre
I have the following code. but it causes exception. using (var context = new
I have the following code which creates a new table. var html = '<table>';
I have the following two lines of code: var BadResult = (100).ToString(B, new CustomFormatter
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
I have the following code: var submitHandler = function ($link, $form, close) { var
I have the following code: var refreshId = setInterval(function() { $(#footad).html('myjshere'); }, 15500); Where

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.