Code for asp.net automatic shutdown
Using system;
Using system. collections. generic;
Using system. componentmodel;
Using system. data;
Using system. drawing;
Using system. text;
Using system. windows. forms;
Using system. runtime. interopservices;
Using system. diagnostics;
Namespace shutdown
{
Public partial class shutdownform: form
{
Public shutdownform ()
{
Initializecomponent ();
}
[Structlayout (layoutkind. sequential)]
Public struct lastinputinfo
{
Public uint cbsize;
Public uint dwtime;
};
[Dllimport ("user32.dll")]
Private static extern bool getlastinputinfo (ref lastinputinfo ii );
Public long getidletime ()
{
Lastinputinfo lastinput = new lastinputinfo ();
Lastinput. cbsize = (uint) system. runtime. interopservices. marshal. sizeof (lastinput );
If (getlastinputinfo (ref lastinput ))
{
Long el = lastinput. dwtime;
Long ui = (environment. tickcount-el );
// Overflow
If (ui <0)
Ui = ui + uint. maxvalue + 1;
Return ui;
}
Else
{
Throw new applicationexception ("timespan ");
}
}
Private void timereffectick (object sender, eventargs e)
{
If (getidletime ()> 5000)
{
Timer1.enabled = false;
Messagebox. show ("5 seconds to"); // no specific significance, convenient debugging
Executecmd ("shutdown.exe-s-f"); // execute the shutdown command
}
}
Public void executecmd (string command)
{
Process proc = new process ();
Proc. startinfo. filename = "cmd.exe ";
Proc. startinfo. useshellexecute = false;
Proc. startinfo. redirectstandarderror = true;
Proc. startinfo. redirectstandardinput = true;
Proc. startinfo. redirectstandardoutput = true;
Proc. startinfo. createnowindow = false;
Proc. start ();
Proc. standardinput. writeline (command );
Proc. standardinput. writeline ("exit ");
}
}
}
// Method 2
If lesssecond <= 0 then process. start ("shutdown-s-t 0 ")
Process. start ("shutdown.exe", "-s-t 0 ");
%>