Example Code

Sleep, Shutdown, Restart and Lock Windows Timer

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview

This program will either Sleep, Shutdown, Lock or Restart your Windows PC in a specified amount of time. This VI uses the producer/consumer design pattern with events and the System Exec V or Call Library Function Node to either sleep, shutdown, restart or lock your PC.

 

Description

This simple event driven VI uses the System Exec VI or Call Library Function Node to either sleep, shutdown, restart or lock a windows PC.

Call the "LockWorkstation" method within the "user32.dll" using a "Call Library Function Node"

Call the "SetSuspendState" method within the "powrprof.dll" using a "Call Library Function Node". Needs parameters 0,1,0.

It is an extension to my previous community example: Sleep, Shutdown, Restart and Lock Windows programmatically

It was designed so you can change the command as the program is counting down.  So you can change the command after it has started counting down.  This gives that the consumer loop will constantly poll the notifier using Get Notifier Status Function.

 

Requirements

  • LabVIEW 2012 (or compatible)

 

Steps to Implement or Execute Code

  1. Start the VI.
  2. Enter the time you would like for your PC to wait before shutting down, locking, restarting or sleeping.
  3. Select the command you would like to execute when the time runs out (the default is Shutdown).
  4. Click start and watch it count down and then carry out your desired task.

 

Additional Information or References

Front Panel

139fp.PNG

 

VI Snippet

139.png 

Tested on windows 7 and written in LabVIEW 2011 with silver buttons.  I have uploaded 4 versions.  Some have the shutdown, restart, sleep and lock code disabled.  It has been replaced using a disable diagram structure and a One Button Dialog Function for testing purposes.

  1. Saved in LabVIEW 2011 with the Sleep, Shutdown, Restart and Lock code disabled.
  2. Saved in LabVIEW 2011 with the Sleep, Shutdown, Restart and Lock code enabled.
  3. Saved in LabVIEW 8.6 with the Sleep, Shutdown, Restart and Lock code enabled.
  4. Saved in LabVIEW 8.6 with the Sleep, Shutdown, Restart and Lock code disabled.

 

**This document has been updated to meet the current required format for the NI Code Exchange.**

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.