程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#啟動和停止windows服務

C#啟動和停止windows服務

編輯:C#入門知識

 

<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("停止").css("color", "red");  
          changeShowStatus();  
          //setTimeout(changeShowStatus, 6000);  
      }  
      else if (data == "end") {  
          $("#serverStatus").text("啟動").css("color", "green");  
          changeShowStatus();  
          //setTimeout(changeShowStatus, 6000);  
      }  
      else if (data == "NoNormalEnd") {  
          $("#serverStatus").text("啟動").css("color", "green");  
          changeShowStatus();  
      }  
      else if (data == "empty") {  
          alert('服務不存在!');  
      }  
      else if (data == "startfail") {  
          alert('啟動失敗!');  
          $("#serverStatus").text("啟動").css("color", "green");  
          changeShowStatus();  
      }  
      else if (data == "stopfail") {  
          alert("停止失敗!");  
          $("#serverStatus").text("停止").css("color", "red");  
          changeShowStatus();  
      }  
      else {  
          alert('操作失敗!' + data);  
          window.location.reload();  
      }  
  }  
  $(function () {  
      $("#serverStatus").click(function () {  
          var txt = $("#serverStatus").text();  
          if (txt == "停止") {  
              showLoading("服務正在停止......");  
              $("#spnServerStatus").text("正在停止...");  
              $.post("Ajax/ServerHandler.ashx", { "action": "stop" }, isValidServerStatus);  
          }  
          else if (txt == "啟動") {  
              showLoading("服務正在啟動......");  
              $("#spnServerStatus").text("正在啟動...");  
              $.post("Ajax/ServerHandler.ashx", { "action": "start" }, isValidServerStatus);  
          }  
      });  
  });  
  </script>  

  一般處理程序如下:  
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  
              {  
                  //開啟服務     
                      
                          if (CommonClass.StartWindowsService(serverName))  
                          {  
                              context.Response.Write("run");  
                          }  
                          else  
                          {  
                              context.Response.Write("startfail");  
                          }  
              }  
              catch (Exception ex)  
              {  
                  context.Response.Write("提示:"+ex.Message);  
              }  
          }  
          else if (action == "stop") //停止服務  
          {  
              try  
              {  
                      if (CommonClass.StopWindowsService(serverName))  
                      {  
                          //Thread.Sleep(6000*3);  
                          context.Response.Write("end");  
                      }  
                      else  
                      {  
                          context.Response.Write("stopfail");  
                      }  
              }  
              catch (Exception ex)  
              {  
                  if (ex.Message == "超時時間已到而操作尚未完成。")  
                  {  
                      context.Response.Write("提示:" + ex.Message);  
                  }  
                  else  
                  {  
                      context.Response.Write("NoNormalEnd");  
                  }  
              }  
          }  
      }  
  
      public bool IsReusable  
      {  
          get  
          {  
              return false;  
          }  
      }  
  }  

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved