C#启动和停止windows服务的实例代码
C#启动和停止windows服务的实例代码
发布时间:2016-12-28 来源:查字典编辑
摘要:复制代码代码如下:functionshowLoading(desc){$("body").append(""+desc+"");//aler...

复制代码 代码如下:

<script type="text/javascript">

function showLoading(desc) {

$("body").append("<div id="processingdiv"><div> <div><div><span><img src='../img/progress.gif' /></span><span>" + 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;

}

}

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类