我有这个代码,我想在Button1_click中调用,请帮助我。
protected void Button1_Click(object sender, EventArgs e)
{
}
public static void RestartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try { int millisec1 = Environment.TickCount;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
// count the rest of the timeout
int millisec2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds - (millisec2-millisec1));
service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch { }
}
答案 0 :(得分:1)
您可以调用方法:
protected void Button1_Click(object sender, EventArgs e)
{
RestartService("serviceName", 3000);
}
您必须使用您的值填充参数serviceName和Timeout。