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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:45:19+00:00 2026-06-15T00:45:19+00:00

Following is my code , the wait all method is always waiting . class

  • 0

Following is my code , the wait all method is always waiting .

  class Program
    {
        static StreamWriter _fileStream;
        static void Main(string[] args)
        {
            _fileStream = File.CreateText(@"C:\Praveen\HelloThread.txt");
            List<ManualResetEvent> meList = new List<ManualResetEvent>();
            ManualResetEvent currentEvent = new ManualResetEvent(true);
            ManualResetEvent nextEvent = new ManualResetEvent(false);
            meList.Add(currentEvent);
            int length = 10;
            Data data = null;
            Console.WriteLine("Writing started...");
            for (int i = 0; i < length; i++)
            {
                data = new Data { CurrentEvent = currentEvent, Number = i, NextEvent = nextEvent };
                ThreadPool.QueueUserWorkItem(PrintMsg, data);
                meList.Add(nextEvent);
                currentEvent = nextEvent;
                nextEvent = nextEvent = new ManualResetEvent(false);
            }

            CloseAll(meList);
            Console.ReadLine();
        }

        private static void CloseAll(List<ManualResetEvent> meList)
        {
            Console.WriteLine("Requested to close all...");
            while (WaitHandle.WaitAll(meList.ToArray()))
            {

            }

            Console.WriteLine("Done with the writing...");
        }

        private static void PrintMsg(object state)
        {

            Data data = state as Data;

            data.CurrentEvent.WaitOne();

            string msg = "Hello times...";
            for (int j = 0; j < 5; j++)
            {
                _fileStream.WriteLine(msg + data.Number);
                Console.WriteLine(msg + data.Number);
            }           
            data.NextEvent.Set();
        }
    }

    public class Data
    {
        public ManualResetEvent CurrentEvent { get; set; }
        public ManualResetEvent NextEvent { get; set; }
        public int Number { get; set; }
    }

What’s going on , why it’s always waiting , any idea ?

  • 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-15T00:45:20+00:00Added an answer on June 15, 2026 at 12:45 am

    You have an infinite loop in your CloseAll method. WaitAll returns true when all events are signaled. You do not need the empty while loop, just add the line:

    WaitHandle.WaitAll(meList.ToArray());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code :- public class UsingWait1{ public static void main(String... aaa){ CalculateSeries
Following code is generated by a for loop. <form action=saveresponse.php method=POST name=mainForm> <input class=cbox_yes
All the variations of wait(...) are throwing the below exception from the following code.
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
I have the following utility: class Worker { public void DoWorkAsync(Action callback) { Action
I have the following code: #include<stdio.h> #include<semaphore.h> #include<pthread.h> sem_t semr; void* func(void* i) {
I have implement the following code to create a SSL server socket. public void
Here is my code public class ThreadLearn{ private static class Check implements Runnable{ public
So I have the following code: import java.lang.Thread; import java.lang.Integer; class MyThread extends Thread

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.