Click here to Skip to main content
Licence CPOL
First Posted 2 Dec 2011
Views 4,239
Downloads 168
Bookmarked 9 times

Keep Alive the Machine - No Sleep

By | 7 Dec 2011 | Article
This app will keep the machine running

Introduction

This is a very simple application that keeps the computer from sleeping. Nothing very fancy, but it seems useful when you cannot control the power options (managed by your domain admin) but you want your PC to stay awake occasionally.

Using the Code

    internal static class NativeMethods
    {
        // Import SetThreadExecutionState Win32 API and necessary flags
        [DllImport("kernel32.dll")]
        public static extern uint SetThreadExecutionState(uint esFlags);
        public const uint ES_CONTINUOUS = 0x80000000;
        public const uint ES_SYSTEM_REQUIRED = 0x00000001;
    }

public partial class MainForm : Form
    {
        private uint m_previousExecutionState;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            // Set new state to prevent system sleep (note: still allows screen saver)
            m_previousExecutionState = NativeMethods.SetThreadExecutionState(
                NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);
            if (0 == m_previousExecutionState)
            {
                MessageBox.Show("Failed to set system state.",  
        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                // No way to recover; fail gracefully
                Close();
            }
        }

        protected override void OnClosed(System.EventArgs e)
        {
            base.OnClosed(e);

            // Restore previous state
            if (0 == NativeMethods.SetThreadExecutionState(m_previousExecutionState))
            {
                // No way to recover; already exiting
            }
        }
    }

Credits

License

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

About the Author

Maruf Maniruzzaman

Software Developer
KAZ Software Ltd. Bangladesh.
Bangladesh Bangladesh

Member

Have completed BSc in Computer Science & Engineering from Shah Jalal University of Science & Technology, Sylhet, Bangladesh (SUST).
 
Story books (specially Masud Rana series), tourism, songs and programming is most favorite.
 
Blog: http://blog.kuashaonline.com
 
The CodeProject group on facebook

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 PinmemberKim Togo0:34 6 Dec '11  
GeneralMy vote of 4 PinmemberAddy Tas3:12 3 Dec '11  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web04 | 2.5.120517.1 | Last Updated 7 Dec 2011
Article Copyright 2011 by Maruf Maniruzzaman
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid