2016-12-06 70 views
0

我有這段代碼,我想調用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(在代碼的最後)? –

回答

1

您可以直接調用方法:

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

你必須填寫參數服務名和超時與你的價值觀是肯定的。

+0

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

相關問題