How do do an async ServiceController.WaitForStatus?

£可爱£侵袭症+ 提交于 2019-12-07 06:44:46

问题


So ServiceController.WaitForStatus is a blocking call. How can it be done Task/Async manner?


回答1:


The code for ServiceController.WaitForStatus is:

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();
    }
}

This can be converted to a task based api using the following:

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();
        }
    }
}

Or with support for a 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();
        }
    }
}


来源:https://stackoverflow.com/questions/38236238/how-do-do-an-async-servicecontroller-waitforstatus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!