© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago
Tony Wang

Synchronizing with Monitor

I have found a code snippet that looks like this:

var signal = new object();
var signalSet = false;

// Thread 1
lock (signal)
{
while (!signalSet)
{
Monitor.Wait(signal);
}
}

// Thread 2
lock (signal)
{
signalSet = true;
Monitor.Pulse(signal);
}

I am confused about the while loop. What does it accomplish? Does a simple "if" do the same here?

Found at:
https://stackoverflow.com/questions/1355398/monitor-vs-waithandle-based-thread-sync
Stack Overflow
Monitor vs WaitHandle based thread sync
I was under the impression, after reading this article that it is better to use Monitor/Lock for thread synchronisation as it does not use native resources
Specific quote (from page 5 of the articl...
Monitor vs WaitHandle based thread sync
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Monitor Signaling
C#CC# / help
2y ago
Synchronizing 2 tables's ID between eachother using EntityFramework Core
C#CC# / help
2y ago
All Monitor ScreenShot
C#CC# / help
2y ago
Distributed Observability with Open Telemetry and Azure Monitor (Application Insights)
C#CC# / help
7mo ago