Disable screensaver

theScien

Member
Joined
Jun 14, 2005
Messages
7
Location
London, UK
Programming Experience
10+
[RESOLVED] Disable screensaver in VB.NET

Hi guys, just stumbled across a little annoying problem, I have developed a little screensaver, that I also run by calling the *.scr file directly when I want to leave my workstation to kind of lock it, however when the screen saver kicks in it's designated time and I ended up with two applications running, I solved that by checking and end the new instance, however, say the screen saver is set to go in every 5 mins of inactivity, when I call the *.scr file and leave, then the screensaver will come on in 5 mins, it determines an instance is already running and ends, but it does that every 5 mins, and everytime it does it the CPU spikes (not good if I'm rendering an animation) so the best is to disable the screen saver everytime I call the file directly.

Certain applications do this, in particular video players.

Any ideas how to disable the screensaver from kicking when my application is running, then enabling it again on my app exit? (VB.NET prefered)

Thanks.
 
Last edited:
Well I found the answer to my own question, posting it here now, as others might need it.

My research, showed that Windows looks at a particular registry key value to determine wether to run the screensaver or not, so, I just look up that key and set it to 0 (zero), when my application exits I set it back to 1 (one), observe:


VB.NET:
^Imports Microsoft.Win32


'// This goes on form_load

'// Reads and sets the registry to disable screensaver from kicking
Dim regKey As RegistryKey
Dim regVal As String
regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
regVal = regKey.GetValue("ScreenSaveActive")
If regVal = 1 Then
	regKey.SetValue("ScreenSaveActive", "0")
End If
regKey.Close()


'//This goes on app exit

'// Reads and sets the registry to re-enable screensaver
Dim regKey As RegistryKey
Dim regVal As String
regKey = Registry.CurrentUser.OpenSubKey("Control Panel\Desktop", True)
regVal = regKey.GetValue("ScreenSaveActive")
If regVal = 0 Then
	regKey.SetValue("ScreenSaveActive", "1")
End If
regKey.Close()

You may notice I'm using strings (Dim regVal As String) even thou we are dealing with integers, however if you set it to integer, the registry key will change to a DWORD, when it's a string by default.
 
Back
Top