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.