Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Use EventWaitHandle in Windows for Synchronization

EventWaitHandle is a synchronization primitive used in multi-threaded applications to signal and wait for events. It is crucial for coordinating actions between different threads, ensuring that certain tasks do not proceed until specific conditions are met. In the Windows environment, EventWaitHandle is supported and can be utilized effectively within .NET applications. This article will guide you through the process of creating and using EventWaitHandle in a Windows environment, including practical examples using C# and PowerShell.


Examples:


Example 1: Using EventWaitHandle in C


In this example, we'll create a simple C# application that demonstrates how to use EventWaitHandle to synchronize threads.


1. Create a new C# Console Application:
Open Visual Studio and create a new Console Application project.


2. Add the following code:


using System;
using System.Threading;

class Program
{
static EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);

static void Main()
{
Thread workerThread = new Thread(Worker);
workerThread.Start();

Console.WriteLine("Main thread doing some work...");
Thread.Sleep(2000); // Simulate work
Console.WriteLine("Main thread signaling worker thread to proceed.");
waitHandle.Set(); // Signal the worker thread to proceed

workerThread.Join();
Console.WriteLine("Main thread finished.");
}

static void Worker()
{
Console.WriteLine("Worker thread waiting for signal...");
waitHandle.WaitOne(); // Wait for signal
Console.WriteLine("Worker thread received signal and proceeding.");
// Simulate work
Thread.Sleep(1000);
Console.WriteLine("Worker thread finished.");
}
}

3. Run the application:
When you run the application, you will see how the main thread signals the worker thread to proceed after completing its work.


Example 2: Using EventWaitHandle in PowerShell


While PowerShell does not natively support EventWaitHandle, you can leverage .NET classes within PowerShell scripts to achieve similar synchronization.


1. Create a PowerShell script:


Add-Type -TypeDefinition @"
using System;
using System.Threading;

public class SyncExample
{
public static EventWaitHandle waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset);

public static void Worker()
{
Console.WriteLine("Worker thread waiting for signal...");
waitHandle.WaitOne(); // Wait for signal
Console.WriteLine("Worker thread received signal and proceeding.");
Thread.Sleep(1000); // Simulate work
Console.WriteLine("Worker thread finished.");
}
}
"@

$workerThread = [System.Threading.Thread]::new([System.Threading.ThreadStart] { [SyncExample]::Worker() })
$workerThread.Start()

Write-Output "Main thread doing some work..."
Start-Sleep -Seconds 2 # Simulate work
Write-Output "Main thread signaling worker thread to proceed."
[SyncExample]::waitHandle.Set() # Signal the worker thread to proceed

$workerThread.Join()
Write-Output "Main thread finished."

2. Run the script:
Execute the script in PowerShell to see the synchronization in action.


To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.