C starts and stops the instance code for the windows service

  • 2020-05-19 05:31:39
  • OfStack


<script type="text/javascript">  
      function showLoading(desc) {  
          $("body").append("<div id=\"processingdiv\" style=\"display:none;\"><div class=\"popup\"> <div class=\"popup-body\"><div class=\"loading\"><span style='width:128px; height:128px;'><img src='../img/progress.gif' /></span><span class='spnContent'>" + desc + "</span></div></div></div></div>");  
          //alert($("head").html());    
          $.openPopupLayer({  
              name: "processing",  
              width: 500,  
              target: "processingdiv" 
          });  
      }  
      function hideLoading() {  
          $.closePopupLayer('processing');  
          $("#processingdiv").remove();  
      }    
  function changeShowStatus(){  
      $.post("Ajax/ShowHandler.ashx", { "action": "ChangeStatusShow" }, function (data) {  
          $("#spnServerStatus").text(data);  
          hideLoading();  
      });  
  }  
  var isValidServerStatus = function (data) {  
      if (data == "run") {  
          $("#serverStatus").text(" stop ").css("color", "red");  
          changeShowStatus();  
          //setTimeout(changeShowStatus, 6000);  
      }  
      else if (data == "end") {  
          $("#serverStatus").text(" Start the ").css("color", "green");  
          changeShowStatus();  
          //setTimeout(changeShowStatus, 6000);  
      }  
      else if (data == "NoNormalEnd") {  
          $("#serverStatus").text(" Start the ").css("color", "green");  
          changeShowStatus();  
      }  
      else if (data == "empty") {  
          alert(' The service doesn't exist! ');  
      }  
      else if (data == "startfail") {  
          alert(' Startup failed! ');  
          $("#serverStatus").text(" Start the ").css("color", "green");  
          changeShowStatus();  
      }  
      else if (data == "stopfail") {  
          alert(" Stop failing! ");  
          $("#serverStatus").text(" stop ").css("color", "red");  
          changeShowStatus();  
      }  
      else {  
          alert(' The operation failure !' + data);  
          window.location.reload();  
      }  
  }  
  $(function () {  
      $("#serverStatus").click(function () {  
          var txt = $("#serverStatus").text();  
          if (txt == " stop ") {  
              showLoading(" Service is down ......");  
              $("#spnServerStatus").text(" Is to stop ...");  
              $.post("Ajax/ServerHandler.ashx", { "action": "stop" }, isValidServerStatus);  
          }  
          else if (txt == " Start the ") {  
              showLoading(" The service is starting ......");  
              $("#spnServerStatus").text(" Is starting ...");  
              $.post("Ajax/ServerHandler.ashx", { "action": "start" }, isValidServerStatus);  
          }  
      });  
  });  
  </script> 

1. General processing procedures are as follows:

public class ServerHandler : IHttpHandler  
  {  
      public void ProcessRequest(HttpContext context)  
      {  
          context.Response.ContentType = "text/plain";  
          string action = context.Request["action"];  
          string serverName = QuarrysClass.WindowsServerName;  
          EnumServiceStatus status = CommonClass.GetServiceStatus(serverName);  
          if (string.IsNullOrEmpty(serverName))  
          {  
              context.Response.Write("empty");  
          }  
          if (action == "start")  
          {  
              byte[] ver = new byte[1024];  

              try 
              {  
                  // Open the service      

                          if (CommonClass.StartWindowsService(serverName))  
                          {  
                              context.Response.Write("run");  
                          }  
                          else 
                          {  
                              context.Response.Write("startfail");  
                          }  
              }  
              catch (Exception ex)  
              {  
                  context.Response.Write(" prompt :"+ex.Message);  
              }  
          }  
          else if (action == "stop") // Stop the service   
          {  
              try 
              {  
                      if (CommonClass.StopWindowsService(serverName))  
                      {  
                          //Thread.Sleep(6000*3);  
                          context.Response.Write("end");  
                      }  
                      else 
                      {  
                          context.Response.Write("stopfail");  
                      }  
              }  
              catch (Exception ex)  
              {  
                  if (ex.Message == " The timeout has expired and the operation has not yet completed. ")  
                  {  
                      context.Response.Write(" prompt :" + ex.Message);  
                  }  
                  else 
                  {  
                      context.Response.Write("NoNormalEnd");  
                  }  
              }  
          }  
      }  

      public bool IsReusable  
      {  
          get 
          {  
              return false;  
          }  
      }  
  }   

Related articles: