0

I have a reboot button in my c# app.
This is my code snippet:
System.Diagnostics.Process.Start("shutdown.exe", "-r -t 15");

My problem is that I'm getting a default Windows message: enter image description here

I know that I can change the content of the message via -c "some string",
How can I remove it completely? (I have my own message to show)

Thanks!

nafarkash
  • 359
  • 6
  • 24
  • Use the `Windows API` calls described in a [related post](http://stackoverflow.com/questions/102567/how-to-shut-down-the-computer-from-c-sharp). – Axel Kemper Dec 07 '16 at 12:02

2 Answers2

0

You can try:

System.Diagnostics.Process.Start("shutdown.exe", "-r -t 15 -f");

-f Force running applications to close without forewarning users. The -f parameter is implied when a value greater than 0 is specified for the -t parameter.

nick_gabpe
  • 5,113
  • 5
  • 29
  • 38
  • Thanks for the answer, but it doesn't help. I'm still getting the same message box – nafarkash Dec 07 '16 at 13:03
  • 1
    You can try `System.Threading.Thread.Sleep(15000); System.Diagnostics.Process.Start("shutdown.exe", "-r -t 0 -f");` Did this help? – nick_gabpe Dec 07 '16 at 13:16
0

Thanks to @nick_gabpe I managed to bypass the problem.
Not sure if it's the neatest answer, but it works..

I have a MessageBox with "Restart now" button, then I used a BackgroundWorker for my problem:

public static void RebootMachine()
    {

        BackgroundWorker worker = new BackgroundWorker();            
        worker.DoWork += (o, ea) =>
        {
            System.Threading.Thread.Sleep(15000);
        };
        worker.RunWorkerCompleted += (o, ea) =>
        {                
            System.Diagnostics.Process.Start("shutdown.exe", "-f -r -t 0");
        };            

        worker.RunWorkerAsync();

        DialogResult result = MsgBox.Show("The device will be initialized in 15 seconds",
            "Restarting device", MsgBox.Buttons.RestartNow, MsgBox.Icon.Info, MsgBox.AnimateStyle.FadeIn);

        if (result == DialogResult.Yes)
        {
            System.Diagnostics.Process.Start("shutdown.exe", "-f -r -t 0");    
        }

    }
nafarkash
  • 359
  • 6
  • 24
  • Just wanted another thread on the background so my UI could still work, and this is the first thing that came through my mind. Do you have a nicer way to do that? – nafarkash Dec 07 '16 at 14:10
  • Does the code *actually* execute `Thread.Sleep`, or is this just a demonstration of code that you have in there to do actual work? It is unclear to me why you care about your UI remaining responsive when you're forcing an immediate shutdown. – Cody Gray - on strike Dec 07 '16 at 14:15
  • When I click the reboot button on my UI, I have a message saying windows will be initialized in 15 seconds. In the same messageBox, I have a restart now button- which, well, restarts windows now.. – nafarkash Dec 07 '16 at 14:44