本文整理汇总了C#中ServiceControllerStatus.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceControllerStatus.Equals方法的具体用法?C# ServiceControllerStatus.Equals怎么用?C# ServiceControllerStatus.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceControllerStatus
的用法示例。
在下文中一共展示了ServiceControllerStatus.Equals方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: setServiceStatus
private void setServiceStatus(ServiceControllerStatus status)
{
if (serviceController.Status.Equals(status)) //already in the wanted status
{
OnProgress(processIdx + 1);
}
else
{
try
{
TimeSpan timeoutSec = TimeSpan.FromSeconds(timeout);
bool tryAgain = false;
int tryCount = 0;
do
{
try
{
if (status.Equals(ServiceControllerStatus.Running))
serviceController.Start(); //TODO: support arguments
else
serviceController.Stop();
}
catch (InvalidOperationException e)
{
//in some cases, the service status might be changed although the exception is thrown
Thread.Sleep(TimeSpan.FromSeconds(5)); //wait a little bit time to try again
serviceController.Refresh();
if (serviceController.Status.Equals(status)) //equal
tryAgain = false;
else if (tryCount < TOTAL_TRY) //not equal, not reach total
tryAgain = true;
else //when not equal to status, AND reach total try
throw e;
tryCount++;
}
} while (tryAgain);
serviceController.WaitForStatus(status, timeoutSec);
Console.WriteLine(serviceController.Status.ToString());
//check the status to make sure it is started properly
if (serviceController.Status.Equals(status))
{
OnProgress(processIdx + 1);
}
else
{
throw new InvalidOperationException(serviceController.ServiceName + " cannot be changed to status " + status.ToString());
}
}
//TODO: there should be configuration for the app
//when error occurs to a service, should it stop or continue to next service?
catch (InvalidOperationException e)
{
OnError(e);
}
catch (System.ServiceProcess.TimeoutException e)
{
OnError(e);
}
}
}