2016-12-06 5 views
0

私はこのコードをButton1_click pls help meで呼びたいと思います。どのように保護されたvoidでpublic 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); 
} 

あなたは確かに自分の価値観を持つパラメータserviceNameをとタイムアウトを記入する必要があります。

+0

はい、今すぐ動作します。 :) –

関連する問題