Click here to Skip to main content
13,200,202 members (60,706 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 2 Oct 2006

Turn Monitors On and Off from a .NET Console Application

, 2 Oct 2006
Rate this:
Please Sign up or sign in to vote.
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

You may also be interested in...

Comments and Discussions

QuestionHow to break screensaver ? Pin
sanong2-Oct-06 7:43
membersanong2-Oct-06 7:43 
AnswerRe: How to break screensaver ? Pin
DrJaymz2-Oct-06 9:08
memberDrJaymz2-Oct-06 9:08 
GeneralRe: How to break screensaver ? Pin
sanong2-Oct-06 22:38
membersanong2-Oct-06 22:38 
GeneralShouldn't broadcast the message Pin
Leo Davidson2-Oct-06 4:54
memberLeo Davidson2-Oct-06 4:54 
Raymond Chen (one of the Windows developers) 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'm just as guilty of this mistake as you are. In fact, I still haven't gotten around to updating
my similar C++ program to do the right thing, since I've been too busy and it does still work, at least for now, on Windows XP, on my machine... Eventually I'll fix it, though, and I figured I'd pass on the info.)

I wrote my thing so I could turn off my monitors when I go to bed without having to wait for the screensaver to kick in. I added the ability to specify a timeout so I could turn the light off and still have 10 seconds of bright screen so I could see my way into bed. Works like a charm. Smile | :)

All the best,
GeneralRe: Shouldn't broadcast the message Pin
DrJaymz2-Oct-06 5:59
memberDrJaymz2-Oct-06 5:59 
GeneralRe: Shouldn't broadcast the message Pin
DrJaymz2-Oct-06 6:03
memberDrJaymz2-Oct-06 6:03 
GeneralRe: Shouldn't broadcast the message [modified] Pin
Victor Vogelpoel9-Oct-06 20:18
memberVictor Vogelpoel9-Oct-06 20:18 
GeneralRe: Shouldn't broadcast the message Pin
DrJaymz10-Oct-06 1:11
memberDrJaymz10-Oct-06 1:11 
GeneralRe: Shouldn't broadcast the message Pin
Victor Vogelpoel10-Oct-06 4:32
memberVictor Vogelpoel10-Oct-06 4:32 
GeneralRe: Shouldn't broadcast the message Pin
Leo Davidson26-Jul-08 21:44
memberLeo Davidson26-Jul-08 21:44 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 2 Oct 2006
Article Copyright 2006 by DrJaymz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid