How to determine that a screensaver is running? - windows

One solution that one could suggest is to periodically look for a special 'Screen-saver' desktop, and if it's present then count it as a running screensaver.
But apparently this is not the case for all screensavers as with some the mentioned desktop is always present.
Are there more reliable solutions for this?

See the WINAPI function SystemParametersInfo() with the SPI_GETSCREENSAVERRUNNING parameter. It's the only documented way to detect whether the screen saver is currently running. (You can also find out if a screen saver is even enabled, get and set the timeout value, and start the screen saver using this function.)

I think the key is to identify the screensaver by its window class ("WindowsScreenSaverClass"). I found this page
How do I start, detect and stop screen savers? that has some code examples and explanations.

HOW TO: Determine If Screen Saver Is Running by Using Visual Basic 6.0
I know this is for VB6, but it's bound to be a good place to start.

This is my implementation of screen saver event detection.
Notes:
SPI_GETSCREENSAVERRUNNING detects Windows Event id 4802 and 4803 which weren't accurate in my case, see my problem hence user idle time calculation is added
GetLastInputInfo apparently doesn't work if process is a service.

search processes for a process with .scr in its .MainModule.FileName

Related

take a screenshot of a desktop created using createdesktop api

i am using the createdesktop api to create a desktop and i would like to take a screenshot or send input mouse/keyboard without dispalying the desktop to the user.any ideeas on how to implement this???
The short answer that I've found is that you can't. You can't take a screenshot of an inactive desktop because there are no paint calls because there are no visible windows to redraw.
You can do a SwitchDesktop() call, screen shot, then SwitchDesktop() back. The user won't notice it, but you likely won't get much in the screen shot because in this short time the windows haven't had time to redraw.
Another thing is, you have to make a new thread to call SetThreadDesktop(). If you use your main thread to do so, it will fail when using a GUI application. SetThreadDesktop() fails when you have a window in the current desktop.
SysInternals has an application to manage multiple desktops (like the linux desktop switch). When your about to pick a desktop to switch to it will show you a thumbnail of the desktop. This thumbnail is not live, it is captured by the last known full redraw when the user is in that desktop. In short, if SysInternals can't do a live screenshot I doubt any of us will.
This is of course based on my own research of this exact feature. If someone has actually gotten it to work I'd love to know so I can't use it too!
Edit: This won't work for invisible desktops, I've looked to my old code, and I see that I needed that for catching screenshot of active desktop (which was not 'WinSta0\Default'), to get handle of active user desktop I've used OpenInputDesktop.
+1 ThievingSix you are right.
Sorry everyone for my misunderstanding.
You need to use SetThreadDesktop (if you are creating desktop by CreateDestkop, then you have handle for it which you pass to SetThreadDesktop). After switching desktop for thread, you can catch screenshot. Good idea would be revert to previous desktop for thread (to not 'break' other/future code).
var
lOldDesktop: HDESK;
begin
lOldDesktop:= GetThreadDesktop(GetCurrentThreadId);
try
if not SetThreadDesktop(ADesktop) then // pass handle to your desktop, or dekstop handle obtained from OpenInputDesktop
{error handle, like RaiseLastOSError or Exit(False)};
// your screenshot/input/mouse code here
finally
if lOldDesktop<> 0 then // GetThreadDesktop can fail (I don't know condition when this GetThreadDesktop(GetCurrentThreadId) could fail)
SetThreadDesktop(lOldDesktop); // revert thread to previous desktop
end;
end;
This code should run in non-main thread, as ThievingSix pointed because SetThreadDesktop can fail in that case. Safe way is spawn thread to make screenshot.
PS. I'm not sure if this will work with "send input mouse/keyboard" (it should), but for screenshot works.
Edit:
More on sessions, window stations, desktops here http://blogs.technet.com/b/askperf/archive/2007/07/24/sessions-desktops-and-windows-stations.aspx
Desktop tool (SysInternals) - http://technet.microsoft.com/en-us/sysinternals/cc817881

How to extend the app timeout in Win 7?

My program is for rendering an animation clip, but every frame has to be computed before rendered. It seems that if the computation takes seconds, windows starts to recognize my process as "not-responding" and refuses to render anything any more, although in the Task Manager, the process itself appears to switch between "running" and "not-responding" all the time.
I have been searching solutions online for a while. A lot people suggested to delete some registry entries:
HKEY_USERS.DEFAULT\Control Panel\Desktop\HungAppTimeOut
HKEY_USERS.DEFAULT\Control Panel\Desktop\WaitToKillAppTimeOut
HKEY_CURRENT_USER\Control Panel\Desktop\HungAppTimeOut
HKEY_CURRENT_USER\Control Panel\Desktop\WaitToKillAppTimeOut
HKEY_CURRENT_USER\Control Panel\Desktop\WaitToKillServiceTimeOut
But I could not find those entries. And according to the descriptions, all those entries mentioned are about controlling how long the system waits after the user requires to end a process or log off to kill a process or service. So they are not what exactly I want. I guess there must be some threshold to control when the system should start to consider one process as "not responding" and I want to change that threshold into a slightly bigger one.
Thanks for your attention and time, any suggestion is welcome!
You should avoid hanging the UI in the first place by doing your processing/computation in a background thread, rather than on the UI thread. If you're using WinForms or WPF, use a BackgroundWorker.
See here for some BGWorker examples: WPF Threads: Build More Responsive Apps With The Dispatcher
I'm using Microstation to build a model, so this is a similar process. About half way through building the model everything freezes up and adds the "application not responding" message to the title.
Disabling the Desktop Window Manager Session Manager worked for me, however in a enterprise environment this is not the best solution, as common users don't have rights to access the services panel.
Try disabling Desktop Window Manager Session Manager from services. Worked for me. That also disabled aero theme by the way. Win7 really looks fancy but it has to operate normally.

Receive screensaver notification

I want to receive a notification in my C++ application when a screensaver is about to start. I tried listening to WM_SYSCOMMAND messages with wParam == SC_SCREENSAVE which some people think should do the trick.
That didn't work. Spy++ even showed that my window didn't receive any WM_SYSCOMMAND message. Interesting thing is when I turned off the monitor I did receive the message with wParam == SC_MONITORPOWER. Am I understanding it wrong? Or did I just miss something?
Edit: For testing I used the default windows screensaver (the one with windows logo).
It appears that I will receive the SC_SCREENSAVE message only when my window has focus. The way around this is to set global hook. That would require me to put the callback function in a separate DLL and there is also this scary message about hooks slowing down the system so I decided to drop the idea of responding to screensaver start.
This is a relatively complex task (although it would be nice if it were easy).
Some of these tests you'll find online only work if your window is in focus. If it's running in the background it may not receive such messages.
Other tests rely on a screensaver program running (check the currently set screensaver, and then watch the process list to see if it's active) but don't work if you go into powersave mode, or if your screensaver is a black screen (ie, no program, just monitor off).
I don't believe there's an ideal way to do this. You might want to go back to the beginning and think more carefully about why you need to detect this state, and what you are trying to accomplish. You might need a different solution.
Probably my answer comes too late.
The MSDN handles screensavers under "Legacy".
On a notebook they waste battery and on a PC they are also useless.
It is better to turn the monitor off than letting it show a screensaver.
As you don't explain exactly what you want to do I don't know if you really need the notification BEFORE the saver starts or if it is enough to get notified when it already has just started.
In the latter case it is easy.
Write a thread that periodically checks:
BOOL b_SaverRunning;
SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, &b_SaverRunning, 0);

screensaver hurts CUDA performance?

I've noticed that the running times of my CUDA kernels are almost tripled the moment the screensaver kicks in. This happens even if it's the blank screensaver.
Oddly enough, this appears to have nothing to do with the power settings. When I disable the screen saver and let the screen power off, the performance stays the same. When I set the "Turn off monitor" to "Never" and lets the screen saver kick in, it happens.
Why does this happen?
Is there a way to counteract this phenomena?
Is there a way to tell windows not to kick in the screen saver? (How do media players do it?)
I'm working on XP SP2 x64
Firstly, its interesting that CUDA is so impacted.
But here is the recipe in win32 for avoiding the screensaver kicking in:
A normal approach is to send yourself 'fake' key presses occasionally using the SendInput API, to reset the inactivity timer that triggers the screensaver.
It is possible to stop applications doing this, however, using the SPI_SETBLOCKSENDINPUTRESETS parameter for SystemParametersInfo.
Another approach is just to turn the screensaver off programmatically, using SPI_SETSCREENSAVEACTIVE for SystemParametersInfo. However, this is a global setting for the whole user - what if two programs use this overlapping? Try to avoid this!

Create a Program that Sits in The Windows Taskbar and, When Activated, Stops the Screensaver From Starting

I don't really know where to begin. Let's start with the stupid questions:
What language should I use for this? What is suited for the task at hand?
Next, the real ones:
Is there a way to stop the screensaver from starting, short of changing the cursor position? If not, will changing the cursor position even work?
SetThreadExecutionState will prevent the screensaver from coming on or the machine from automatically going to sleep if you pass the ES_CONTINUOUS and ES_DISPLAY_REQUIRED flags.
I wrote an app awhile ago that does exactly what you are asking for. It runs as an icon in the System Tray, not the Taskbar, and uses a global message hook to disable the WM_SYSCOMMAND/SC_SCREENSAVE notification from reaching any applications. If that notification does not reach the DefWindowProc() function, the screen saver will never run.
Your program does not need to be visible in the task bar at all.
You don't even need a program at all, if you can disable the screensaver in the registry.
What you want to do can perhaps be achieved by sending a MOUSE_MOVE event to the desktop window. If you want to use C# (the only language I am current with right now), you can look at this article, but maybe a simple C program using the WinAPI is better suited for this task.
.NET will easily allow you to put an application in the system tray (checkout the NotifyIcon object in System.Windows.Forms.Controls).
I believe you can use the SetCursorPos (http://msdn.microsoft.com/en-us/library/ms648394(VS.85).aspx) API call to prevent the screen saver, just make sure you set them to the current location so you don't actually move the mouse.

Resources