Find Number and resolution to all monitors

In C#: Screen Class Represents a display device or multiple display devices on a single system. You want the Bounds attribute. foreach(var screen in Screen.AllScreens) { // For each screen, add the screen properties to a list box. listBox1.Items.Add(“Device Name: ” + screen.DeviceName); listBox1.Items.Add(“Bounds: ” + screen.Bounds.ToString()); listBox1.Items.Add(“Type: ” + screen.GetType().ToString()); listBox1.Items.Add(“Working Area: ” + … Read more

Lock (Monitor) internal implementation in .NET

The Wikipedia article has a pretty good description of what a “Monitor” is, as well as its underlying technology, the Condition Variable. Note that the .NET Monitor is a correct implementation of a condition variable; most published Win32 implementations of CVs are incorrect, even ones found in normally reputable sources such as Dr. Dobbs. This … Read more

Is it ok to read a shared boolean flag without locking it when another thread may set it (at most once)?

It is never OK to read something possibly modified in a different thread without synchronization. What level of synchronization is needed depends on what you are actually reading. For primitive types, you should have a look at atomic reads, e.g. in the form of std::atomic<bool>. The reason synchronization is always needed is that the processors … Read more

Monitor vs Mutex

Since you haven’t specified which OS or language/library you are talking about, let me answer in a generic way. Conceptually they are the same. But usually they are implemented slightly differently Monitor Usually, the implementation of monitors is faster/light-weight, since it is designed for multi-threaded synchronization within the same process. Also, usually, it is provided … Read more

What’s the meaning of an object’s monitor in Java? Why use this word?

but I am puzzled why use word “the object’s monitor” instend of “the object’s lock”? See ulmangt’s answer for links that explain the term “monitor” as used in this context. Note that: “Monitors were invented by Per Brinch Hansen and C. A. R. Hoare, and were first implemented in Brinch Hansen’s Concurrent Pascal language.” (Source: … Read more

Monitor vs lock

Eric Lippert talks about this in his blog: Locks and exceptions do not mix The equivalent code differs between C# 4.0 and earlier versions. In C# 4.0 it is: bool lockWasTaken = false; var temp = obj; try { Monitor.Enter(temp, ref lockWasTaken); { body } } finally { if (lockWasTaken) Monitor.Exit(temp); } It relies on … Read more