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?


-Manajit
 
ManajitAsked:
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?).
0
 
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
0
 
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?
0
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)

http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=36953

http://www.dreamincode.net/forums/showtopic33948.htm

C. System.Runtime.InteropServices Pinvoke

http://bytes.com/groups/net-c/251367-shutdown-my-computer-using-c

D. System Management

http://www.geekpedia.com/code36_Shut-down-system-using-Csharp.html

Best regards.
0
 
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.

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

Open in new window

0
 
ReneGeCommented:
@LeeTutor

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

I suggest to close the question by splitting points.

Cheers,
Rene
0
 
LeeTutorretiredCommented:
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.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
The question has been answered.
0
 
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).

Modalot
Community Support Moderator
0
 
ReneGeCommented:
Thanks for the points

Cheers,
Rene
0
 
ReneGeCommented:
@Manajit

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

Cheers,
Rene
0
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.