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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:44:26+00:00 2026-05-16T19:44:26+00:00

I have written a sample program below. class Program { static int x =

  • 0

I have written a sample program below.

class Program
    {
        static int x = 2;

        static void Main(string[] args)
        {
            Console.WriteLine("Thread ID {0} and Main Called!", Thread.CurrentThread.ManagedThreadId);

            ThreadPool.QueueUserWorkItem(Count, args);
            ThreadPool.QueueUserWorkItem(Count2, args);

            Console.WriteLine("Thread ID {0} and Main Done!", Thread.CurrentThread.ManagedThreadId);
            Console.ReadLine();
        }
        static void Count(object args)
        { 
            for (int i = 0; i < 10; i++)
            {
                x = x + 2;
                Console.WriteLine("Thread ID {0} AND Count 1: " + x, Thread.CurrentThread.ManagedThreadId);
            }
        }
        static void Count2(object args)
        {
            for (int i = 0; i < 10; i++)
            {
                x = x + 2;
                Console.WriteLine("Thread ID {0} AND Count 2: " + x, Thread.CurrentThread.ManagedThreadId);
            }
        }
    }

When calling the Count method using ThreadPool.QueueUserWorkItem, I noticed that Main is finished before the Count method completes and Count2 method gets tangled up with Count Method.

Is there anyway for Main (and Count2) to wait until the Count method has compleleted? I do not want to use a lock nor Thread.Sleep (since I dont know how long the Count operations will take). I have read somewhere async calls or WAIT is used for this time of situations.

Any ideas?

  • 1 1 Answer
  • 1 View
  • 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-16T19:44:26+00:00Added an answer on May 16, 2026 at 7:44 pm

    I think AutoResetEvent is what you are after

    **EDIT: **
    Here is the modified code (untested). I used AutoResetEvent rather than Manual in this example

    class Program
    {
        static int x = 2;
        // Define an array with two AutoResetEvent WaitHandles.
        static WaitHandle[] waitHandles = new WaitHandle[] 
        {
            new AutoResetEvent(false),
            new AutoResetEvent(false)
        };
    
    
    
        static void Main(string[] args)
        {
            Console.WriteLine("Thread ID {0} and Main Called!", Thread.CurrentThread.ManagedThreadId);
    
            ThreadPool.QueueUserWorkItem(new WaitCallback(Count), waitHandles[0]);
            ThreadPool.QueueUserWorkItem(new WaitCallback(Count2), waitHandles[1]);
            WaitHandle.WaitAll(waitHandles);
    
    
            Console.WriteLine("Thread ID {0} and Main Done!", Thread.CurrentThread.ManagedThreadId);
            Console.ReadLine();
        }
        static void Count(object args)
        {
            AutoResetEvent are = (AutoResetEvent)args;
    
            for (int i = 0; i < 10; i++)
            {
                x = x + 2;
                Console.WriteLine("Thread ID {0} AND Count 1: " + x, Thread.CurrentThread.ManagedThreadId);
            }
            are.Set();
    
        }
        static void Count2(object args)
        {
            AutoResetEvent are = (AutoResetEvent)args;
    
            for (int i = 0; i < 10; i++)
            {
                x = x + 2;
                Console.WriteLine("Thread ID {0} AND Count 2: " + x, Thread.CurrentThread.ManagedThreadId);
            }
            are.Set();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written a sample program.below: #include<iostream> #include<string> #include<set> using namespace std; int main()
Below I have written a sample program that I have written to learn about
I have written sample program which uses XSLT to generate HTML response. Check below
I have written a sample program : #include<iostream> #include<set> #include<conio.h> using namespace std; int
I have written some sample program and DLL to learn the concept of DLL
I have written grammar for a language (sample code below) //this is a procedure
I have written a small Sample program to see about Eclipse Conditional Break Points
I am working on Email client application. I have written one sample program for
I have written a sample console application to test backgroundworker using one of the
I have written a simple OpenGL program in C++ that displays a line joining

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.