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. Question on shutting down another program and wating for it to exit.

Question on shutting down another program and wating for it to exit.

Scheduled Pinned Locked Moved C#
questioncsharpsysadmin
5 Posts 4 Posters 1 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.
  • Z Offline
    Z Offline
    Zundee
    wrote on last edited by
    #1

    Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks

        public void restartserver(string serverrs, string serverloc)
        {
    
            Process p = Process.GetCurrentProcess();
            Process\[\] servername = Process.GetProcessesByName(serverrs);
            IntPtr serverHandle = FindWindow(null, serverloc);
            if (serverHandle == IntPtr.Zero)
            {
                if (serverrs == "arcemu-world")
                    {
                        btnWorldStatus.Text = "World Server is not running";
                    }
                    else
                    {
                        btnLogonStatus.Text = "Logon Server is not running";
                    }
                }
                else
                {
    
                    SetForegroundWindow(serverHandle);
                    SendKeys.SendWait("Shutdown{ENTER}");
                    FreeConsole();
                    bool mytrue = true;
                    while (mytrue)
                    {
                        if (p.HasExited)
                        {
                            mytrue = false;
    
                            if (serverrs == "arcemu-world")
                            {
                                btnWorldStatus.Text = "Closing....";
                            }
                            else
                            {
                                btnLogonStatus.Text = "Closing....";
                            }
    
                        }
                    }
                    
                }
                
            
        }
    
    T 2 D 3 Replies Last reply
    0
    • Z Zundee

      Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks

          public void restartserver(string serverrs, string serverloc)
          {
      
              Process p = Process.GetCurrentProcess();
              Process\[\] servername = Process.GetProcessesByName(serverrs);
              IntPtr serverHandle = FindWindow(null, serverloc);
              if (serverHandle == IntPtr.Zero)
              {
                  if (serverrs == "arcemu-world")
                      {
                          btnWorldStatus.Text = "World Server is not running";
                      }
                      else
                      {
                          btnLogonStatus.Text = "Logon Server is not running";
                      }
                  }
                  else
                  {
      
                      SetForegroundWindow(serverHandle);
                      SendKeys.SendWait("Shutdown{ENTER}");
                      FreeConsole();
                      bool mytrue = true;
                      while (mytrue)
                      {
                          if (p.HasExited)
                          {
                              mytrue = false;
      
                              if (serverrs == "arcemu-world")
                              {
                                  btnWorldStatus.Text = "Closing....";
                              }
                              else
                              {
                                  btnLogonStatus.Text = "Closing....";
                              }
      
                          }
                      }
                      
                  }
                  
              
          }
      
      T Offline
      T Offline
      tonyonlinux
      wrote on last edited by
      #2

      Maybe This[^] will help you..

      Z 1 Reply Last reply
      0
      • Z Zundee

        Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks

            public void restartserver(string serverrs, string serverloc)
            {
        
                Process p = Process.GetCurrentProcess();
                Process\[\] servername = Process.GetProcessesByName(serverrs);
                IntPtr serverHandle = FindWindow(null, serverloc);
                if (serverHandle == IntPtr.Zero)
                {
                    if (serverrs == "arcemu-world")
                        {
                            btnWorldStatus.Text = "World Server is not running";
                        }
                        else
                        {
                            btnLogonStatus.Text = "Logon Server is not running";
                        }
                    }
                    else
                    {
        
                        SetForegroundWindow(serverHandle);
                        SendKeys.SendWait("Shutdown{ENTER}");
                        FreeConsole();
                        bool mytrue = true;
                        while (mytrue)
                        {
                            if (p.HasExited)
                            {
                                mytrue = false;
        
                                if (serverrs == "arcemu-world")
                                {
                                    btnWorldStatus.Text = "Closing....";
                                }
                                else
                                {
                                    btnLogonStatus.Text = "Closing....";
                                }
        
                            }
                        }
                        
                    }
                    
                
            }
        
        2 Offline
        2 Offline
        224917
        wrote on last edited by
        #3

        Is it spinning inside the while loop while (mytrue)? Also how did you verify the server has shutdown?

        1 Reply Last reply
        0
        • Z Zundee

          Forgive my newbie question. I am trying to learn C#. I have utterly confused myself in trying to restart a server console. I do not have the server start on this yet but it locks up every time I restart and I am not real sure way. It shutsdown ok but then never makes the check if the process has exited. Can anyone give me a nudge in the right direction? Thanks

              public void restartserver(string serverrs, string serverloc)
              {
          
                  Process p = Process.GetCurrentProcess();
                  Process\[\] servername = Process.GetProcessesByName(serverrs);
                  IntPtr serverHandle = FindWindow(null, serverloc);
                  if (serverHandle == IntPtr.Zero)
                  {
                      if (serverrs == "arcemu-world")
                          {
                              btnWorldStatus.Text = "World Server is not running";
                          }
                          else
                          {
                              btnLogonStatus.Text = "Logon Server is not running";
                          }
                      }
                      else
                      {
          
                          SetForegroundWindow(serverHandle);
                          SendKeys.SendWait("Shutdown{ENTER}");
                          FreeConsole();
                          bool mytrue = true;
                          while (mytrue)
                          {
                              if (p.HasExited)
                              {
                                  mytrue = false;
          
                                  if (serverrs == "arcemu-world")
                                  {
                                      btnWorldStatus.Text = "Closing....";
                                  }
                                  else
                                  {
                                      btnLogonStatus.Text = "Closing....";
                                  }
          
                              }
                          }
                          
                      }
                      
                  
              }
          
          D Offline
          D Offline
          dan sh
          wrote on last edited by
          #4

          1. You are checking if your current process has exited instead of checking if the process you are killing has exited or not. 2. You are not using the Process array you have created (in the code block posted). So why even retrieve it? Following code might help you out:

            Process\[\] processList = Process.GetProcessesByName("proccessName");
          
            MessageBox.Show("Killing processes...");
            try {
              foreach (Process process in processList) {
                process.Kill();
                process.WaitForExit();
              }
          
              MessageBox.Show("Processes killed successfully.");
          
            }
            catch (Exception exception){
              // Log exception here
              MessageBox.Show("Could not kill on of the processes.");
            }
          

          You can also use the other overload of WaitForExit and provide a suitable time out. In case you use that, then, after the call to the method, check if the process has exited and display the appropriate method if process is still running.

          "No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem

          1 Reply Last reply
          0
          • T tonyonlinux

            Maybe This[^] will help you..

            Z Offline
            Z Offline
            Zundee
            wrote on last edited by
            #5

            Thanks for your help. After taking a look at the information provided I got it.

            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