you cant tell how many seconds are left in the timer. the timer is always active until you disable or stop it. it will keep ticking until you stop it.
one way would be to create a global variable of your "secondsLeft", set it to the starting number of seconds left, then on the timer tick event, subtract 1 from it then update the label, and finally if secondsLeft is 0 then execute and stop timer
why would it not help on Windows Server Editions of course it will work, if you have permission to shut down the machine, even if its the local machine.
you would be better off using the shutdown utility than to P/Invoke as you should avoid using P/Invoke as much as possible as it not only slows down performance but can also defeat the purpose of the .NET Base Class libraries.
the way of using the shutdown utility is as follows:
why would it not help on Windows Server Editions of course it will work, if you have permission to shut down the machine, even if its the local machine.
Sorry Ilyas,
Actually I have only Windows XP to test it right now. I tried to do the same usig shutdown exe on Windows Server 2000 at my work about 1 year back but there was some issue. May be I'm wrong so if I'm so I beg your pardon! You may be right if you have tested it on Windows Server...
Thank u guys for all ur help but how do I make a label's text change according to how many seconds r left in the timer For instance I have a timer and a label, I want the timer to countdown for 3 seconds and the label to countdown like 3, 2, 1 and then the shutdown method activated.
Wait hold on, I have the timer's interval set to 3000 which is 3 seconds correct My timer is enabled. This is the code that I have so far:
private void ShutdownTimer_Tick(object sender, EventArgs e)
I think, second condition was true, while working on Windows Server 2000 I missed shutdown.exe in System32 directory but again I would say I may be wrong because it was a long time ago and I dont remember what stopped me doing it!
How do I programmatically shutdown the computer?
Mikael Olesen
you cant tell how many seconds are left in the timer. the timer is always active until you disable or stop it. it will keep ticking until you stop it.
one way would be to create a global variable of your "secondsLeft", set it to the starting number of seconds left, then on the timer tick event, subtract 1 from it then update the label, and finally if secondsLeft is 0 then execute and stop timer
private int secondsLeft = 3;
..
..
//timer tick (interval = 1000)
if (this.secondsLeft == 0)
{
this.theTimer.Stop();
//execute shutdown
}
else
{
this.theLabel.Text = "seconds left: " + this.secondsLeft.ToString();
secondsLeft--;
}
Will Merydith
Alright I tried it and it didn't work, here's my code, I don't know if I did it wrong or what:
private void ShutdownTimer_Tick(object sender, EventArgs e){
System.Diagnostics.
Process.Start("Shutdown", "-s"); private int secondsLeft = 3; //timer tick (interval = 1000) if (this.secondsLeft == 0){
this.ShutdownTimer.stop(); //execute shutdown}
else{
this.label2.Text = "seconds left: " + this.secondsLeft.ToString();secondsLeft--;
}
}
private void Form1_Load(object sender, EventArgs e){
label2.Text = ShutdownTimer.Interval.ToString() +
" Seconds";}
Pramod
why would it not help on Windows Server Editions of course it will work, if you have permission to shut down the machine, even if its the local machine.
you would be better off using the shutdown utility than to P/Invoke as you should avoid using P/Invoke as much as possible as it not only slows down performance but can also defeat the purpose of the .NET Base Class libraries.
the way of using the shutdown utility is as follows:
shutdown /m \\computerName /s /t 5
/m specifies the machine name
/s is to shutdown the machine (/r is to reboot the machine)
/t is the number of seconds until it starts to shutdown the machine
System.Diagnostics.ProcessStartInfo thePSI = new System.Diagnostics.ProcessStartInfo("shutdown");
thePSI.Arguments = "/m \\\\computerName /s /t 5";
System.Diagnostics.Process.Start(thePSI);
I believe this utility is available from Windows 2000 and higher, except for Windows ME
Chiarigos
public class ClassName
{
private int secondsLeft = 3;
//...
//...
private void ShutdownTimer_Tick(object sender, EventArgs e)
{
if (this.secondsLeft == 0){
this.ShutdownTimer.stop(); System.Diagnostics.Process.Start("Shutdown", "-s");}
else{
this.label2.Text = "seconds left: " + this.secondsLeft.ToString();secondsLeft--;
}
}
private void Form1_Load(object sender, EventArgs e){
label2.Text = ShutdownTimer.Interval.ToString() +
" Seconds";this.ShutDownTimer.Start();
}
}
DragonC#
Well you will have to use the API function InitiateSystemShutdownEx... For a full reference of the function visit http://msdn2.microsoft.com/en-gb/library/aa376874.aspx.. You have many parameters that u can supply.
If this helped you pls mark as answered
greenhorn
Sorry Ilyas,
Actually I have only Windows XP to test it right now. I tried to do the same usig shutdown exe on Windows Server 2000 at my work about 1 year back but there was some issue. May be I'm wrong so if I'm so I beg your pardon! You may be right if you have tested it on Windows Server...
Best Regards,
Rizwan Ahmed
DevilDog74
Pintoo Khaira
{
System.Diagnostics.
Process.Start("Shutdown", "-s");}
private void Form1_Load(object sender, EventArgs e){
label2.Text = ShutdownTimer.Interval.ToString() +
" Seconds";}
Is there anything we can do from here to just set the label to refresh and keep counting down according to how many seconds are left
amiable
no worries my friend :-)
As I said, it depends on a couple of factors:
Arjun B
I think, second condition was true, while working on Windows Server 2000 I missed shutdown.exe in System32 directory but again I would say I may be wrong because it was a long time ago and I dont remember what stopped me doing it!
Thank you and Best Regards,
Rizwan
Jason D. Camp
Using shutodown.exe will not help on Windows Server Editions. The better way to do with Win32 APIs, Here is the sample:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=243746&SiteID=1
Best Regards,
Rizwan
Alexander Olekhnovich
Also take a look at this class WindowsController which alow yo to restart or shutdown the operating system from your applications
another option you can run shoutdown process by write some thing like
System.Diagnostics.Process.Start("Shutdown", "-s ");
to know more about other command at command prompt write shoutdown /
Hossam Abdel Wahab
well you have 2 choices. Either create another timer to do the count down or use the same timer but alter it to the suggestion I had posted.
in your existing timer, set the interval to 1000 milliseconds (1 second)
Then create that global variable I was talking about and set it to 3 seconds by default
then in here, in the tick event, pretty much copy the code I had and paste it in.