async ServiceController.WaitForStatus 怎么做?
How do do an async ServiceController.WaitForStatus?
所以ServiceController.WaitForStatus是一个阻塞调用。如何才能做到Task/Async方式?
ServiceController.WaitForStatus
的代码是:
public void WaitForStatus(ServiceControllerStatus desiredStatus, TimeSpan timeout)
{
DateTime utcNow = DateTime.UtcNow;
this.Refresh();
while (this.Status != desiredStatus)
{
if (DateTime.UtcNow - utcNow > timeout)
{
throw new TimeoutException(Res.GetString("Timeout"));
}
Thread.Sleep(250);
this.Refresh();
}
}
可以使用以下方法将其转换为基于 api 的任务:
public static class ServiceControllerExtensions
{
public static async Task WaitForStatusAsync(this ServiceController controller, ServiceControllerStatus desiredStatus, TimeSpan timeout)
{
var utcNow = DateTime.UtcNow;
controller.Refresh();
while (controller.Status != desiredStatus)
{
if (DateTime.UtcNow - utcNow > timeout)
{
throw new TimeoutException($"Failed to wait for '{controller.ServiceName}' to change status to '{desiredStatus}'.");
}
await Task.Delay(250)
.ConfigureAwait(false);
controller.Refresh();
}
}
}
或支持 CancellationToken
public static class ServiceControllerExtensions
{
public static async Task WaitForStatusAsync(this ServiceController controller, ServiceControllerStatus desiredStatus, TimeSpan timeout, CancellationToken cancellationToken)
{
var utcNow = DateTime.UtcNow;
controller.Refresh();
while (controller.Status != desiredStatus)
{
if (DateTime.UtcNow - utcNow > timeout)
{
throw new TimeoutException($"Failed to wait for '{controller.ServiceName}' to change status to '{desiredStatus}'.");
}
await Task.Delay(250, cancellationToken)
.ConfigureAwait(false);
controller.Refresh();
}
}
}
所以ServiceController.WaitForStatus是一个阻塞调用。如何才能做到Task/Async方式?
ServiceController.WaitForStatus
的代码是:
public void WaitForStatus(ServiceControllerStatus desiredStatus, TimeSpan timeout)
{
DateTime utcNow = DateTime.UtcNow;
this.Refresh();
while (this.Status != desiredStatus)
{
if (DateTime.UtcNow - utcNow > timeout)
{
throw new TimeoutException(Res.GetString("Timeout"));
}
Thread.Sleep(250);
this.Refresh();
}
}
可以使用以下方法将其转换为基于 api 的任务:
public static class ServiceControllerExtensions
{
public static async Task WaitForStatusAsync(this ServiceController controller, ServiceControllerStatus desiredStatus, TimeSpan timeout)
{
var utcNow = DateTime.UtcNow;
controller.Refresh();
while (controller.Status != desiredStatus)
{
if (DateTime.UtcNow - utcNow > timeout)
{
throw new TimeoutException($"Failed to wait for '{controller.ServiceName}' to change status to '{desiredStatus}'.");
}
await Task.Delay(250)
.ConfigureAwait(false);
controller.Refresh();
}
}
}
或支持 CancellationToken
public static class ServiceControllerExtensions
{
public static async Task WaitForStatusAsync(this ServiceController controller, ServiceControllerStatus desiredStatus, TimeSpan timeout, CancellationToken cancellationToken)
{
var utcNow = DateTime.UtcNow;
controller.Refresh();
while (controller.Status != desiredStatus)
{
if (DateTime.UtcNow - utcNow > timeout)
{
throw new TimeoutException($"Failed to wait for '{controller.ServiceName}' to change status to '{desiredStatus}'.");
}
await Task.Delay(250, cancellationToken)
.ConfigureAwait(false);
controller.Refresh();
}
}
}