?
Solved

A Batch file to shutdown the system

Posted on 2011-05-09
13
Medium Priority
?
357 Views
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?


-Manajit
 
0
Comment
Question by:Manajit
11 Comments
 
LVL 45

Accepted Solution

by:
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?).
0
 
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
0
 
LVL 45

Expert Comment

by:AndyAinscow
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?
0
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!

 
LVL 8

Assisted Solution

by:cubaman_24
cubaman_24 earned 332 total points
ID: 35727243
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
 
LVL 10

Assisted Solution

by:ReneGe
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.

@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
 
LVL 10

Expert Comment

by:ReneGe
ID: 35901314
@LeeTutor

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

I suggest to close the question by splitting points.

Cheers,
Rene
0
 
LVL 59

Expert Comment

by:LeeTutor
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.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 35904034
The question has been answered.
0
 

Expert Comment

by:Modalot
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).

Modalot
Community Support Moderator
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35939216
Thanks for the points

Cheers,
Rene
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 35939234
@Manajit

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

Cheers,
Rene
0

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