Use C # To write a Timed Shutdown program

Source: Internet
Author: User

When I return to the dormitory every night, sometimes I still need to use the lab machine to download and upload something or something. However, if the machine is always on, the service life will be reduced. So I wrote an automatic shutdown program to automatically shut down the machine at midnight every day. This program is easily tested in Win2k. It is easy to say that even if many other programs are running, they can still be shut down normally.

The principle is very simple, so you don't need to put the code up. It is a function that calls an API to shut down the computer. It must be declared first.

[Dllimport ("user32.dll", exactspelling = true, setlasterror = true)]

Internal static extern bool exitwindowsex (INT flg, int rea );

 

Then, we use a timer control to regularly determine whether the current time is equal to the selected shutdown time. If it is equal, execute exitwindowsex and then OK.

 

However, after calling the shutdown API function, you must immediately stop the tick event of timer. Timer1.enabled = false; otherwise, exitwindowsex will be called multiple times. The result is that the computer gives you a blue-blue interface with no content on it and stops responding. Try it yourself. Haha.

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.