当前位置: 移动技术网 > IT编程>开发语言>c# > C#启动和停止windows服务的实例代码

C#启动和停止windows服务的实例代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:<script type="text/javascript">        f
复制代码 代码如下:

<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; 
          } 
      } 
  }  

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网