A Batch file to shutdown the system

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?

Who is Participating?
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
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?).
John ClaesSenior .Net Consultant & Technical AnalistCommented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
@ 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?
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

cubaman_24Connect With a Mentor Commented:
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.
ReneGeConnect With a Mentor Commented:
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


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

I suggest to close the question by splitting points.

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.
AndyAinscowFreelance programmer / ConsultantCommented:
The question has been answered.
ModalotEE ModeratorCommented:
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
Thanks for the points


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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.