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);
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?
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! ๐ฅ