Complete C# Tutorial

AutoResetEvent & ManualResetEvent in C# – Master Thread Signaling!

Hey there, C# champion! 👋 Ever wondered how to pause a thread and wait for a signal before it continues?

That’s exactly what AutoResetEvent in Thread and ManualResetEvent in Thread do! 🚀 These tools are super useful when you need threads to coordinate with each other.

Today, we’ll break them down with real-world examples and easy-to-understand code. Let’s go! 🎯

🚀 Why Do We Need AutoResetEvent and ManualResetEvent?

Imagine this:

  • You are waiting for a bus 🚌. The bus will arrive at any time, but you won’t move until it does. (This is like a thread waiting for a signal!)

  • Once the bus arrives, you and others start moving.

In multithreading, sometimes one thread needs to signal another thread to continue.

That’s where AutoResetEvent and ManualResetEvent help! 💡

🔄 What is AutoResetEvent in Thread?

🔹 AutoResetEvent is like a one-time traffic signal.
🔹 When a thread waits, it stops until another thread signals it to continue.
🔹 Once the signal is received, AutoResetEvent automatically resets back to red.

 

Syntax of AutoResetEvent

				
					AutoResetEvent autoEvent = new AutoResetEvent(false);
				
			
  • false means the thread starts in a blocked state.
  • true means the thread starts in a signaled state.

🏆 Example 1: Using AutoResetEvent in Thread

Let’s say Thread A is waiting, and Thread B will signal it to continue.

				
					using System;
using System.Threading;

class Program
{
    static AutoResetEvent autoEvent = new AutoResetEvent(false);

    static void WaitingThread()
    {
        Console.WriteLine("Thread A is waiting for a signal...");
        autoEvent.WaitOne(); // Wait until signaled
        Console.WriteLine("Thread A received the signal and continues...");
    }

    static void Main()
    {
        new Thread(WaitingThread).Start();

        Thread.Sleep(2000); // Simulate some delay
        Console.WriteLine("Thread B sends the signal!");
        autoEvent.Set(); // Signal Thread A
    }
}
				
			

Expected Output

				
					Thread A is waiting for a signal...
Thread B sends the signal!
Thread A received the signal and continues...
				
			

WaitOne() makes Thread A wait until another thread calls Set().

What is ManualResetEvent in Thread?

  • ManualResetEvent is like a pedestrian crossing button. 🚦
  • Once signaled, it stays green until reset manually.
  • Multiple threads can pass through without waiting until we reset it to red.

 

Syntax of ManualResetEvent

				
					ManualResetEvent manualEvent = new ManualResetEvent(false);
				
			

🏆 Example 2: Using ManualResetEvent in Thread

Let’s say multiple threads are waiting, and one thread signals all of them at once.

				
					using System;
using System.Threading;

class Program
{
    static ManualResetEvent manualEvent = new ManualResetEvent(false);

    static void WaitingThread()
    {
        Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} is waiting...");
        manualEvent.WaitOne(); // Wait until signaled
        Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} received the signal!");
    }

    static void Main()
    {
        for (int i = 0; i < 3; i++)
        {
            new Thread(WaitingThread).Start();
        }

        Thread.Sleep(2000); // Simulate delay
        Console.WriteLine("Main thread sends the signal!");
        manualEvent.Set(); // Signal all threads

        Thread.Sleep(2000);
        Console.WriteLine("Resetting the signal...");
        manualEvent.Reset(); // Reset so future calls must wait
    }
}
				
			

Expected Output

				
					Thread 3 is waiting...
Thread 4 is waiting...
Thread 5 is waiting...
Main thread sends the signal!
Thread 3 received the signal!
Thread 4 received the signal!
Thread 5 received the signal!
Resetting the signal...
				
			
  • All threads continue when Set() is called.
  • New threads must wait after Reset().

🤔 AutoResetEvent vs ManualResetEvent – What’s the Difference?

FeatureAutoResetEventManualResetEvent
Signal BehaviorResets automaticallyStays signaled until manually reset
Threads UnblockedOnly one threadAll waiting threads
Best Use CaseOne-time signal for a threadSignaling multiple threads

🚦 Use AutoResetEvent when only one thread needs the signal.
📢 Use ManualResetEvent when multiple threads need the signal at once.

🌍 Real-World Example: Loading Data Before Execution

Let’s say you’re building a game 🎮.

  • The main thread needs to wait until assets are loaded before starting the game.

  • Multiple worker threads are loading assets in the background.

 

Code for Real-World Example

				
					using System;
using System.Threading;

class GameLoader
{
    static ManualResetEvent assetsLoaded = new ManualResetEvent(false);

    static void LoadAssets()
    {
        Console.WriteLine($"Loading assets... (Thread {Thread.CurrentThread.ManagedThreadId})");
        Thread.Sleep(3000); // Simulate loading time
        Console.WriteLine("Assets loaded!");
        assetsLoaded.Set(); // Signal that loading is complete
    }

    static void StartGame()
    {
        Console.WriteLine("Waiting for assets to load...");
        assetsLoaded.WaitOne(); // Wait for assets to be ready
        Console.WriteLine("Game is starting!");
    }

    static void Main()
    {
        new Thread(LoadAssets).Start();
        new Thread(StartGame).Start();
    }
}
				
			

Expected Output

				
					Waiting for assets to load...
Loading assets... (Thread 3)
Assets loaded!
Game is starting!
				
			

Game starts only after assets are loaded!

🎯 Conclusion – Why This is Important?

AutoResetEvent in Thread is great for one-time signals.
ManualResetEvent in Thread is perfect when multiple threads need the signal.
✅ These tools help coordinate threads efficiently.

 

🚀 Next What?

🎉 Awesome work! You now understand AutoResetEvent in Thread and ManualResetEvent in Thread!

But wait, there’s more! 🚀

Next up: Reader-Writer Locks in Threads – Optimize Multithreading Performance! Stay tuned! 🔥

Leave a Comment

six + three =

Share this Doc

AutoResetEvent and ManualResetEvent

Or copy link