Using this example of turning off monitor with JNA I encountered the following problem: Monitor turns off as expected but immediately turns on in just a second. Even if I comment-out part with sleeping and turning monitor on. All the same.
I thought that monitor turns on when control returns to the main thread. But it's not like that. Running this snippet within the new thread leads to the same result.
Also I've tried powershell script doing the same thing and it works as a charm. But not the Java snippet.
What am I doing wrong? Or what's wrong with this at all.
It's possible that "doesn't work as expected" means your expectations are wrong.
Of note, there are some problems with the linked code, which I assume you used verbatim. The SendMessage function is mapped twice. The first mapping is correct, but never used:
LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM,
LPARAM paramLPARAM);
The second mapping substitutes an int
for a WPARAM
and it wouldn't surprise me if some results here were unusual, particularly on a 64-bit system/JVM. This is the version that's called in the code.
LRESULT SendMessageA(HWND paramHWND, int paramInt, int paramInt2,
LPARAM paramLPARAM);
All that aside, the code does work as expected, it activates the power management features to turn the monitor off! The problem with your expectation is that you haven't considered what might turn the monitor back on. Moving or clicking a mouse (or perhaps releasing a pressed mouse button), pressing (or releasing) a key, network activity, or a variety of other things might generate system events which signal the power saving feature to turn the monitor back on. (You probably would see the same results from a powershell snippet; it's just possible your mouse-click/keystroke sequence is different in that case.)