Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Please tell me why this simple shutdown application isn't working

Please tell me why this simple shutdown application isn't working

Scheduled Pinned Locked Moved C#
csharplinqquestionworkspace
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    turbosupramk3
    wrote on last edited by
    #1

    It writes to the log file every 2 seconds, so I know it is looping, but it does not stop the shutdown executable from running. However if I loop it with a batch file it works perfect? Is there a way to have the command text write to the console window as well? Thanks for reading.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Diagnostics;
    using System.Threading;
    using System.IO;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    while (true)
    {
    System.Diagnostics.ProcessStartInfo shutDownBlocker = new ProcessStartInfo();
    shutDownBlocker.FileName = @"c:\windows\system32\shutdown.exe";
    shutDownBlocker.WindowStyle = ProcessWindowStyle.Normal;
    shutDownBlocker.Arguments = " /a";
    shutDownBlocker.CreateNoWindow = false;
    Thread.Sleep(2000);
    File.AppendAllText(@"c:\temp\shutdownlog.txt", "ran command" + System.Environment.NewLine);
    }
    }
    }
    }

    P M D 3 Replies Last reply
    0
    • T turbosupramk3

      It writes to the log file every 2 seconds, so I know it is looping, but it does not stop the shutdown executable from running. However if I loop it with a batch file it works perfect? Is there a way to have the command text write to the console window as well? Thanks for reading.

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Diagnostics;
      using System.Threading;
      using System.IO;

      namespace ConsoleApplication1
      {
      class Program
      {
      static void Main(string[] args)
      {
      while (true)
      {
      System.Diagnostics.ProcessStartInfo shutDownBlocker = new ProcessStartInfo();
      shutDownBlocker.FileName = @"c:\windows\system32\shutdown.exe";
      shutDownBlocker.WindowStyle = ProcessWindowStyle.Normal;
      shutDownBlocker.Arguments = " /a";
      shutDownBlocker.CreateNoWindow = false;
      Thread.Sleep(2000);
      File.AppendAllText(@"c:\temp\shutdownlog.txt", "ran command" + System.Environment.NewLine);
      }
      }
      }
      }

      P Offline
      P Offline
      Praveen Raghuvanshi
      wrote on last edited by
      #2

      As per my understanding to the question, you have only created the object of ProcessStartInfo and assigned some values to it. you need to call the Process.Start() API of Process class with object of ProcessStartInfo as a parameter. Then and only then your 'shutdown.exe' will start and perform its duty. Refer this link for more details and example http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx[^] If you have further query, please elaborate it. Hope this helps!!!

      Praveen Raghuvanshi Software Developer

      T 1 Reply Last reply
      0
      • T turbosupramk3

        It writes to the log file every 2 seconds, so I know it is looping, but it does not stop the shutdown executable from running. However if I loop it with a batch file it works perfect? Is there a way to have the command text write to the console window as well? Thanks for reading.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Diagnostics;
        using System.Threading;
        using System.IO;

        namespace ConsoleApplication1
        {
        class Program
        {
        static void Main(string[] args)
        {
        while (true)
        {
        System.Diagnostics.ProcessStartInfo shutDownBlocker = new ProcessStartInfo();
        shutDownBlocker.FileName = @"c:\windows\system32\shutdown.exe";
        shutDownBlocker.WindowStyle = ProcessWindowStyle.Normal;
        shutDownBlocker.Arguments = " /a";
        shutDownBlocker.CreateNoWindow = false;
        Thread.Sleep(2000);
        File.AppendAllText(@"c:\temp\shutdownlog.txt", "ran command" + System.Environment.NewLine);
        }
        }
        }
        }

        M Offline
        M Offline
        Mirko1980
        wrote on last edited by
        #3

        You are missing System.Diagnostics.Process.Start(shutDownBlocker);

        1 Reply Last reply
        0
        • T turbosupramk3

          It writes to the log file every 2 seconds, so I know it is looping, but it does not stop the shutdown executable from running. However if I loop it with a batch file it works perfect? Is there a way to have the command text write to the console window as well? Thanks for reading.

          using System;
          using System.Collections.Generic;
          using System.Linq;
          using System.Text;
          using System.Diagnostics;
          using System.Threading;
          using System.IO;

          namespace ConsoleApplication1
          {
          class Program
          {
          static void Main(string[] args)
          {
          while (true)
          {
          System.Diagnostics.ProcessStartInfo shutDownBlocker = new ProcessStartInfo();
          shutDownBlocker.FileName = @"c:\windows\system32\shutdown.exe";
          shutDownBlocker.WindowStyle = ProcessWindowStyle.Normal;
          shutDownBlocker.Arguments = " /a";
          shutDownBlocker.CreateNoWindow = false;
          Thread.Sleep(2000);
          File.AppendAllText(@"c:\temp\shutdownlog.txt", "ran command" + System.Environment.NewLine);
          }
          }
          }
          }

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          On top of what the other people said, this little technique you're using will only stop a shutdown IF it was initiated with the SHUTDOWN.EXE command. If you shutdown the system with Start -> Shutdown, or some other method other than Shutdown.exe, this code won't do a thing to stop it.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          M T 2 Replies Last reply
          0
          • D Dave Kreskowiak

            On top of what the other people said, this little technique you're using will only stop a shutdown IF it was initiated with the SHUTDOWN.EXE command. If you shutdown the system with Start -> Shutdown, or some other method other than Shutdown.exe, this code won't do a thing to stop it.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            M Offline
            M Offline
            Mirko1980
            wrote on last edited by
            #5

            After a quick search I found this Often people try to code complex soluting when the OS already give them the option...

            T 1 Reply Last reply
            0
            • P Praveen Raghuvanshi

              As per my understanding to the question, you have only created the object of ProcessStartInfo and assigned some values to it. you need to call the Process.Start() API of Process class with object of ProcessStartInfo as a parameter. Then and only then your 'shutdown.exe' will start and perform its duty. Refer this link for more details and example http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.aspx[^] If you have further query, please elaborate it. Hope this helps!!!

              Praveen Raghuvanshi Software Developer

              T Offline
              T Offline
              turbosupramk3
              wrote on last edited by
              #6

              Hi Praveen, that looks to be my problem, thank you very much. I'm trying to incorporate it into a service I am building and it just does not work. I am probably overlooking something again, if you have any ideas, I'd love to hear them. Thank you again for the help.

              using System;
              using System.Diagnostics;
              using System.ServiceProcess;
              using System.Threading;
              using System.Windows.Forms;

              namespace WindowsService
              {
              class WindowsService : ServiceBase
              {
              /// <summary>

                  /// Public Constructor for WindowsService.
              
                  /// - Put all of your Initialization code here.
              
                  /// </summary>
                  /// 
                  Thread thread = new Thread(new ThreadStart(shutDownBlocker));
              
                  public WindowsService()
                  {
                      this.ServiceName = "AllinOneService";
                      this.EventLog.Log = "Application";
              
                      // These Flags set whether or not to handle that specific
              
                      //  type of event. Set to true if you need it, false otherwise.
              
                      this.CanHandlePowerEvent = true;
                      this.CanHandleSessionChangeEvent = true;
                      this.CanPauseAndContinue = true;
                      this.CanShutdown = true;
                      this.CanStop = true;
                      shutDownBlocker();
                      thread.Start();
                  }
              
                  /// <summary>
              
                  /// The Main Thread: This is where your Service is Run.
              
                  /// </summary>
              
                  static void Main()
                  {
                      ServiceBase.Run(new WindowsService());
                      
                  }
              
                  /// <summary>
              
                  /// Dispose of objects that need it here.
              
                  /// </summary>
              
                  /// <param name="disposing">Whether
              
                  ///    or not disposing is going on.</param>
              
                  protected override void Dispose(bool disposing)
                  {
                      base.Dispose(disposing);
                  }
              
                  /// <summary>
              
                  /// OnStart(): Put startup code here
              
                  ///  - Start threads, get inital data, etc.
              
                  /// </summary>
              
                  /// <param name="args"></param>
              
                  protected override void OnStart(string\[\] args)
                  {
                      base.OnStart(args);
                      //Thread thread = new Thread(new ThreadStart(shutDownBlocker));
                      
                  }
              
                  /// <summary>
              
                  /// OnStop(): Put your stop code here
              
                  /// - Stop threads, set final data, etc.
              
                  /// </summary>
              
                  protected o
              
              1 Reply Last reply
              0
              • M Mirko1980

                After a quick search I found this Often people try to code complex soluting when the OS already give them the option...

                T Offline
                T Offline
                turbosupramk3
                wrote on last edited by
                #7

                Hi, That's an interesting assumption, but the only thing that has in common with what I am trying to do is the word "shutdown" is in both posts.

                1 Reply Last reply
                0
                • D Dave Kreskowiak

                  On top of what the other people said, this little technique you're using will only stop a shutdown IF it was initiated with the SHUTDOWN.EXE command. If you shutdown the system with Start -> Shutdown, or some other method other than Shutdown.exe, this code won't do a thing to stop it.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak

                  T Offline
                  T Offline
                  turbosupramk3
                  wrote on last edited by
                  #8

                  Hi Dave, I agree ... this is for remote/local calls to that executable on a box via global patches, that needs controlled shutdowns/reboots, but that cannot be in its own AD collection with separate gps.

                  D 1 Reply Last reply
                  0
                  • T turbosupramk3

                    Hi Dave, I agree ... this is for remote/local calls to that executable on a box via global patches, that needs controlled shutdowns/reboots, but that cannot be in its own AD collection with separate gps.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    Unless your patches use SHUTDOWN.EXE to shutdown/restart the machine, your code is still not going to work. The patch has to be told to NOT shutdown the machine, but that's only if the patch supports some kind of switch to tell it not to do so.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    T 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      Unless your patches use SHUTDOWN.EXE to shutdown/restart the machine, your code is still not going to work. The patch has to be told to NOT shutdown the machine, but that's only if the patch supports some kind of switch to tell it not to do so.

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak

                      T Offline
                      T Offline
                      turbosupramk3
                      wrote on last edited by
                      #10

                      Dave, I tested it with a simple batch file that looped with "shutdown /a" every 1000ms and it worked. If you think there is something else that should be coded for as a due diligence type of thing, I'd be happy to incorporate that into the code. Let me know.

                      D 1 Reply Last reply
                      0
                      • T turbosupramk3

                        Dave, I tested it with a simple batch file that looped with "shutdown /a" every 1000ms and it worked. If you think there is something else that should be coded for as a due diligence type of thing, I'd be happy to incorporate that into the code. Let me know.

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #11

                        I told you this only works if the shutdown was initiated with SHUTDOWN.EXE. If something calls ExitWindowsEx, SHUTDOWN /A will NOT abort it. If it works with your test patch, great, that's one test. I can pretty much guarantee that it won't work in all cases. If your patch uses SHUTDOWN.EXE to restart the machine, you'll be OK. Not great, but OK. There will be those cases where your 1 second delay (or whatever you use) will be sufficient for the patch to call SHUTDOWN.EXE and shutdown actually gets around to calling ExitWindowsEx before your piece of code can launch SHUTDOWN /A.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups