Published on

# A little bit about ManualResetEvent

Authors

I'm not sure how often people run into situations where the ManualResetEvent is needed, but I have a few times. System.Threading.ManualResetEvent provides an easy way to allow cross-thread communication and to let other threads know when something has completed. Most of the time that I've needed it, I have a property in a class that is loaded in another thread, but I want to prevent access to the property until it is loaded. From what I've seen, this is a great time to use the ManualResetEvent.

It is easiest to explain with some code snippets. Here's how you create the ManualResetEvent.

Private dataLock As New ManualResetEvent(False)


To wait on the signal, use this code:

_dataLock.WaitOne()


Once your background work is completed, you can call Set like so:

_dataLock.Set()


Here's a full code sample that shows how it would work and also gives a short example on the BackgroundWorker.

Imports System.ComponentModel

Private _dataLock As New ManualResetEvent(False)

Sub Main()

Dim wkr As New BackgroundWorker

wkr.RunWorkerAsync("Get to work!")

Write("Waiting on the lock...")
_dataLock.WaitOne()
Write("we're back!")

Console.WriteLine(vbNewLine & "Press any key to continue...")
End Sub

Private Sub wkr_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs)

Write("DoWork just received this value: " & CStr(e.Argument))

e.Result = "DoWork finished!"

Write("DoWork is done so start signaling completion.")
_dataLock.Set()
End Sub

Private Sub wkr_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Dim wkr As BackgroundWorker = DirectCast(sender, BackgroundWorker)
wkr.Dispose()
End Sub

Private Sub Write(ByVal s As String)