如何在受保护的void中调用public void

时间:2016-12-06 06:36:25

标签: c# c#-4.0 c#-3.0

我有这个代码,我想在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 {  } 
}

1 个答案:

答案 0 :(得分:1)

您可以调用方法:

protected void Button1_Click(object sender, EventArgs e)
{
   RestartService("serviceName", 3000);
}

您必须使用您的值填充参数serviceName和Timeout。