Small software that prevents the system from automatically sleeping, with C # Production Process

Source: Internet
Author: User

Sometimes, when downloading something, you don't want your computer to automatically sleep, so you can enable the exit mode. This will not only save energy and environmental protection, but also cause a headache when you really want to sleep.

Changed itself, disabled the exit mode, and will enter the sleep mode after 30 minutes of no operation. But what should I do during the download? It's also idle, so I wrote this thing:

Added the wake-up function when the lid is closed.

(Well, this option is not actually used)

The program is at the end.

 

The following is implementation (in fact, one of the purposes of writing this program is to learn C #......) :

(So-called)Is to call this API (MSDN introduction ):

EXECUTION_STATE WINAPI SetThreadExecutionState(  _In_  EXECUTION_STATE esFlags);

This API allows a program to notify the system to use certain resources to prevent the system from sleep or disabling the monitor.

The esFlags parameter is a combination of the following options:ES_AWAYMODE_REQUIRED,ES_CONTINUOUS,ES_DISPLAY_REQUIRED,ES_SYSTEM_REQUIRED.

Simply understand the options. ES_CONTINUOUS indicates that this setting will take effect until the next API call. In most cases, you only need to call the API once with this option added. (This API is for each thread. As long as the thread does not exit, the option set with CONTINUOUS will always take effect ).

If the ES_CONTINUOUS option is used separately, the sleep policy is restored.

 

Using System. runtime. interopServices; // according to the API prototype, convert the type declaration of C # To [DllImport ("kernel32.dll")] static extern uint SetThreadExecutionState (uint esFlags ); // The constant const uint Limit = 0x00000040 used by the options; const uint ES_CONTINUOUS = 0x80000000; const uint ES_DISPLAY_REQUIRED = 0x00000002; const uint ES_SYSTEM_REQUIRED = 0x00000001;

C # All method variables must be declared in a class. I declare these things to a class called Public. After the statement is declared, you can directly call this method.

 

This program can be selected through the main interface and the tray pop-up menu, so you need to synchronize the status of these two places, Checkbox to automatically hook, cancel the cancellation, and so on. In order to write code, you do not need to consider these things.OptionClass ). You can set options through this class, or register a delegate in it. When the options change, these delegates are called. At the same time, this class will automatically notify the system when setting options. Note: In the destructor, this API is called once with the ES_CONTINUOUS parameter to restore the original sleep configuration.

 

The main interface is the pull control that handles the event. Note: The option value of the Checkbox may be changed because of mouse clicks, so select the listener.ClickEvent. To process events with less code, I useDictionaryBind each CheckBox with an option value. Then, use the same event processing function. In the function, determine the Checkbox sent based on the sender, and then determine the value to be set based on the Dictionary. Register a delegate to the Option class and change the Checkbox to the correct status when the Option changes.

Hide the window directly when minimizedRather than narrowing down the taskbar: You can select to listenResizeEvent to determine whether the form is in the minimized state in the event. If yes, the form is hidden. More thoroughly, it is to reload the message processing function of the Form class and process the minimum message by yourself.

protected override void WndProc(ref Message m){const int WM_SYSCOMMAND = 0x112;const int SC_CLOSE = 0xF060;const int SC_MINIMIZE = 0xF020;const int SC_MAXIMIZE = 0xF030;if (m.Msg == WM_SYSCOMMAND){if (m.WParam.ToInt32() == SC_MINIMIZE){this.Hide();return;}}base.WndProc(ref m);}

 

Tray: UseYyiconControl. Note: This control must be set to ICON to display. AddContextMenuStripObject as the pop-up menu when right-clicking. You can set the ContextMenuStrip attribute of the tray control to this menu when you click the tray. Here, because I need to listen to mouse events and show the main window when I click the button, I can right-click the event processing function. If you right-click the menu, you can call the Show method of the menu.

Then the menu content. Create severalToolStripMenuItemObject, and use ContextMenuStrip. Items. Add (...) Add these items to the menu. Each Item can listen to mouse-click events, or you can set its Checked attribute to display the hooks before the item text. Add to split lineToolStripSeparatorObject.

Dynamic tray icon: In order to write less Code such as reading files, you can directly add the tray icon to the resource file of the program. The method is inProject PropertiesSelectResourcesThen you can add the desired resources. To access these resources.Properties. Resources. For example, System. Drawing. Bitmap bitmap1 = myProject. Properties. Resources. Image01 (refer to MSDN ). Finally, select the corresponding icon Based on the option status.

Tray disappears: If you directly execute Application. Exit (), the tray will not disappear automatically. You can move the mouse over the tray. To display the tray, runDispose ()Method.

 

Only the tray is displayed when the program starts running, but the main form is not displayed.. When I found this problem during dog search, many new users drank a pot. My solution is:To start a program, run the code of the tray control instead of the code of the main form.Go to the Main function and find that the last execution of the Main function isApplication. Run (...)This method. At first, I tried to fill in a policyicon object in the parameter of this method, but it didn't work. Then try to create a new policyicon object directly with new, the tray will be displayed at this time, but the program will quit immediately. Finally, we found that this method has a version without parameter overloading, and the program will not exit after execution ......

 

: In the project properties, selectApplicationsTab to set the icon.

Sleep Terminator: http://files.cnblogs.com/h46incon/SleepPreventer.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.