Click here to Skip to main content
15,124,353 members
Articles / Programming Languages / Visual Basic
Posted 2 Oct 2006


38 bookmarked

Turn Monitors On and Off from a .NET Console Application

Rate me:
Please Sign up or sign in to vote.
3.83/5 (7 votes)
2 Oct 2006CPOL2 min read
Turn monitors on and off from a .NET Console Application.


I needed to be able to turn the monitors on and off at will, based on a set time. I could have written a bunch of code that allowed me to set the schedule etc., but instead, I wrote this console app. The app runs on the command line, and accepts a couple of arguments so that its then a simple case of setting a schedule via the Windows scheduler. If you put the EXE in the Windows system32 folder, you can call it directly, so to turn the monitors off at night, you'd just call [monitors off], and to turn on in the morning, you'd call [monitors on]. Obviously, a test is helpful, since by definition you cannot see what is happening! [Monitors test].

To do this, you will need to call on the Windows API, and a function called SendMessage supports all sorts of weird and wonderful things, so we need to import this function call. The SendMessage function takes arguments that are simple constants, which can be found if you're lucky on the MSDN website.

Using the Code

Const SC_MONITORPOWER As Integer = &HF170
Const WM_SYSCOMMAND As Short = &H112S

Sub Main()
    Dim instr As String = Command()
    Select Case 
      'Make sure it matches - and look at the commandline switch 
      Case "off"
      Case "on"
      Case "test"
            'turn off monitor 
            'wait 10 seconds
            'turn on monitor 
      Case Else
            MsgBox("Usage, [on]/[off]/[test] ", _
                   MsgBoxStyle.Information, _
                   "Need Command Switch")
    End Select
End Sub

_ Private Function SendMessage( ByVal Handle As Int32, _ 
          ByVal wMsg As Int32, ByVal wParam As Int32, _ 
          ByVal lParam As Int32) As Int32

End Function

Sub StandBy()
End Sub

Sub TurnOff() 
End Sub

Sub TurnOn()
End Sub

Points of Interest

This code works with dual monitors, and you can use it via VNC - which is handy if you are running a kiosk or information display. This code works fine, but you might want to look at Raymond Chen's code (one of the Windows developers). He says that it's wrong to broadcast the monitor power messages. Instead, you should create your own window, post the message to it (only), then run a message pump, and finally exit. I would argue that they should write the documentation of these mystical functions a little better, but you might like to look here for more information: As far as I can gather, it's safe to use this broadcast for this message, but if you delve into the SendMessage() function, you need to be careful, as all top level windows will process the message which could lead to some interesting issues.


  • V1.00: Works fine, I cobbled it together from some code written in C, and I adapted it for .NET; presumably, you can import into C# easily.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

QuestionHow to break screensaver ? Pin
sanong2-Oct-06 8:43
Membersanong2-Oct-06 8:43 
AnswerRe: How to break screensaver ? Pin
DrJaymz2-Oct-06 10:08
MemberDrJaymz2-Oct-06 10:08 
GeneralRe: How to break screensaver ? Pin
sanong2-Oct-06 23:38
Membersanong2-Oct-06 23:38 
GeneralShouldn't broadcast the message Pin
Leo Davidson2-Oct-06 5:54
MemberLeo Davidson2-Oct-06 5:54 
GeneralRe: Shouldn't broadcast the message Pin
DrJaymz2-Oct-06 6:59
MemberDrJaymz2-Oct-06 6:59 
GeneralRe: Shouldn't broadcast the message Pin
DrJaymz2-Oct-06 7:03
MemberDrJaymz2-Oct-06 7:03 
GeneralRe: Shouldn't broadcast the message [modified] Pin
Victor Vogelpoel9-Oct-06 21:18
MemberVictor Vogelpoel9-Oct-06 21:18 
GeneralRe: Shouldn't broadcast the message Pin
DrJaymz10-Oct-06 2:11
MemberDrJaymz10-Oct-06 2:11 
GeneralRe: Shouldn't broadcast the message Pin
Victor Vogelpoel10-Oct-06 5:32
MemberVictor Vogelpoel10-Oct-06 5:32 
GeneralRe: Shouldn't broadcast the message Pin
Leo Davidson26-Jul-08 22:44
MemberLeo Davidson26-Jul-08 22:44 
In case anyone finds this through searching, I thought I'd mention that I've updated my own program (ages ago in fact) to do the right thing and the source is available, though it may only be useful to people writing C++ or at least non-.Net apps:

ScreenSave.exe with C++ source[^]

Hope someone finds it useful.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.