How to call a method from a button in C#
I have a button in C#:
private void button15_Click(object sender, EventArgs e)
{
StartService();
}
and I try to call the method:
public static void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
}
catch
{
// ...
}
}
But I am not sure if the call method is corr开发者_C百科ect on the button
Well, the StartService
seems to take two parameters: string and integer and when calling it you aren't passing any. The compiler is probably already telling you this. Generally reading compiler error messages helps.
Also when calling a static method you might want to specify the class name where this method is defined (for more clarity):
SomeClass.StartService("some name of a service", 1000);
You would need to provide the parameters for startservice. At the moment, Im very doubtful this would compile.
Eg
StartService("MyService",20000);
Your program won't compile because the StartService
method is expecting two parameters (serviceName
and timeoutMilliseconds
).
精彩评论