A Batch file to shutdown the system

Posted on 2011-05-09
Medium Priority
Last Modified: 2012-05-11
I want to shutdown the PC from a c# application.The command i have used is
"-s -t 30"

But In some PCs it din't work.So i have changed the command for forceful shutdown. The new command is
"-s -f -t 30"

Now the query is,
I am persisting some information in Form.Dispose() ,So that i can start from the same state once i restart. But by changing to forceful shutdown whether Form.Dispose will be called ?
will this fix work?  
Is ther any other better mechanism for the same?

Question by:Manajit
LVL 45

Accepted Solution

AndyAinscow earned 336 total points
ID: 35726635
If you close the app immediately after issuing the shutdown command it ought to be OK.  (30 seconds, I would have thought, is enough to save the information you want to save - or is it?).
LVL 10

Expert Comment

by:John Claes
ID: 35726718
Manajit && AndyAinscow

This will only work if you have Called the command in an other thread so you can immediatly call the ending Methods of your app so your dispose/Persistance is called.

If you use the same thread your app will wait for the shutdown to end its waiting and won't be on time to persist the data

poor beggar
LVL 45

Expert Comment

ID: 35726802
@ poor_beggar


I've just done what I suggested - without any other thread.
I see the app disappear normally from the task manager then about 28 seconds later my system shutsdown.  I don't see why a normal termination of the app, with shutdown pending, will force the app to behave differently and not call any disposing of forms.  ALSO if it did then why should creating a new thread stop the new behaviour?
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Assisted Solution

cubaman_24 earned 332 total points
ID: 35727243
Different methods:

A. System.Diagnostics.Process.Start("Shutdown", "-s -t 10");

B. Windows Management Instrumentation (WMI)



C. System.Runtime.InteropServices Pinvoke


D. System Management


Best regards.
LVL 10

Assisted Solution

ReneGe earned 332 total points
ID: 35737879
By saying this:
"I am persisting some information in Form.Dispose() ,So that i can start from the same state once i restart. But by changing to forceful shutdown whether Form.Dispose will be called ?"

Are you saying that there are some information in something called "Form.Dispose()" that if you forcefully shutdown, you will loose something?  But your challange here would be that you must forcefully shutdown to have a proper shutdown. Correct?

In this case, why don't you try taskkill your app without force and then, when the app is closed, forcefully shutdown.

SET App=YourApp.exe
TASKKILL /im "%App%"
Tasklist | FINDSTR -i "%App%"
IF %errorlevel% == 0 (
	PING -n 5 >NUL
	GOTO Loop
SHUTDOWN -f -s -t 30

Open in new window

LVL 10

Expert Comment

ID: 35901314

Support was extensivly provided without proper follow-up by poster.

I suggest to close the question by splitting points.

LVL 59

Expert Comment

ID: 35904033
I've requested that this question be deleted for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
LVL 45

Expert Comment

ID: 35904034
The question has been answered.

Expert Comment

ID: 35937196
Following an Objection by AndyAinscow, and after Moderator review, there seems to be a better  disposition, as recommended by the contributing Expert(s).

Community Support Moderator
LVL 10

Expert Comment

ID: 35939216
Thanks for the points

LVL 10

Expert Comment

ID: 35939234

Could you please give us some feedback on the help we provided?


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question