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! 🎯
📚 What You Are Going to Learn in This Lesson?
✔️ What is AutoResetEvent in Thread and how does it work?
✔️ What is ManualResetEvent in Thread and how does it differ?
✔️ Real-world scenarios where these are useful.
✔️ Multiple coding examples with clear explanations.
🚀 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);
falsemeans the thread starts in a blocked state.truemeans 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?
ManualResetEventis 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?
| Feature | AutoResetEvent | ManualResetEvent |
|---|---|---|
| Signal Behavior | Resets automatically | Stays signaled until manually reset |
| Threads Unblocked | Only one thread | All waiting threads |
| Best Use Case | One-time signal for a thread | Signaling 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! 🔥
