C#啟動(dòng)和停止windows服務(wù)的實(shí)例代碼
更新時(shí)間:2013年09月04日 14:54:24 作者:
這篇文章介紹了C#啟動(dòng)和停止windows服務(wù)的實(shí)例代碼,有需要的朋友可以參考一下
復(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("啟動(dòng)").css("color", "green");
changeShowStatus();
//setTimeout(changeShowStatus, 6000);
}
else if (data == "NoNormalEnd") {
$("#serverStatus").text("啟動(dòng)").css("color", "green");
changeShowStatus();
}
else if (data == "empty") {
alert('服務(wù)不存在!');
}
else if (data == "startfail") {
alert('啟動(dòng)失敗!');
$("#serverStatus").text("啟動(dòng)").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("服務(wù)正在停止......");
$("#spnServerStatus").text("正在停止...");
$.post("Ajax/ServerHandler.ashx", { "action": "stop" }, isValidServerStatus);
}
else if (txt == "啟動(dòng)") {
showLoading("服務(wù)正在啟動(dòng)......");
$("#spnServerStatus").text("正在啟動(dòng)...");
$.post("Ajax/ServerHandler.ashx", { "action": "start" }, isValidServerStatus);
}
});
});
</script>
一般處理程序如下:
復(fù)制代碼 代碼如下:
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
{
//開啟服務(wù)
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") //停止服務(wù)
{
try
{
if (CommonClass.StopWindowsService(serverName))
{
//Thread.Sleep(6000*3);
context.Response.Write("end");
}
else
{
context.Response.Write("stopfail");
}
}
catch (Exception ex)
{
if (ex.Message == "超時(shí)時(shí)間已到而操作尚未完成。")
{
context.Response.Write("提示:" + ex.Message);
}
else
{
context.Response.Write("NoNormalEnd");
}
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
您可能感興趣的文章:
- C#啟動(dòng)windows服務(wù)方法的相關(guān)問題分析
- C#編寫Windows服務(wù)實(shí)例代碼
- c#創(chuàng)建windows服務(wù)(Windows Services)詳細(xì)步驟
- c#創(chuàng)建windows服務(wù)入門教程實(shí)例
- C#使用windows服務(wù)開啟應(yīng)用程序的方法
- C#編寫Windows服務(wù)程序詳細(xì)步驟詳解(圖文)
- C#創(chuàng)建Windows服務(wù)的實(shí)現(xiàn)方法
- C#創(chuàng)建Windows服務(wù)與服務(wù)的安裝、卸載
- C#創(chuàng)建Windows Service(Windows 服務(wù))的方法步驟
相關(guān)文章
Unity?UGUI的RawImage原始圖片組件使用示例詳解
這篇文章主要為大家介紹了Unity?UGUI的RawImage原始圖片組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07C# 獲取硬盤號(hào),CPU信息,加密解密技術(shù)的步驟
這篇文章主要介紹了C# 獲取硬盤號(hào),CPU信息,加密解密技術(shù)的步驟,幫助大家更好的理解和學(xué)習(xí)c#,感興趣的朋友可以了解下2021-01-01使用C#實(shí)現(xiàn)在word中插入頁眉頁腳的方法
這篇文章主要介紹了使用C#實(shí)現(xiàn)在word中插入頁眉頁腳的方法,是操作Word的常見方法,有一定的學(xué)習(xí)借鑒價(jià)值,需要的朋友可以參考下2014-08-08