如何调用public void protected void

如何调用public void protected void

问题描述:

我有这段代码,我想调用Button1_click请帮助我。如何调用public void protected void

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

你需要2个值'serviceName'和'timeoutMilliseconds' – fubo

+0

基本上@fubo试图告诉你,你可以这样调用它:'RestartService(“somestring”,999);'。假设两个方法在同一个类中。虽然我想知道为什么在那里有一个catch(在代码的最后)? –

您可以直接调用方法:

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

你必须填写参数服务名和超时与你的价值观是肯定的。

+0

是的,它现在工作ty。 :) –