Why does ServiceController.WaitForStatus throws a TimeoutException? - by crip

Status : 

  Won't Fix<br /><br />
		Due to several factors the product team decided to focus its efforts on other items.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.


0
0
Sign in
to vote
ID 436389 Comments
Status Closed Workarounds
Type Suggestion Repros 0
Opened 4/30/2009 6:41:07 AM
Access Restriction Public

Description

I'm using the ServiceContoller for checking if service like spoolsv is running or is going to stop. 

There is the first solution. Use ServiceController.WaitForStatus(desiredStatus) and wait for with the timout infinity. This works, but if I'm going to shutdown my application the servicecontroller will be disposed and I get an ObjectDisposedException. 

The second solution is with no ObjectDisposedException: 
Using the WaitForStatus(ServiceControllerStatus desiredStatus, TimeSpan timeout) Method and looping I always get a TimeoutException. 

Sign in to post a comment.
Posted by Microsoft on 6/8/2009 at 2:09 AM
Hi cribby,

Thanks for the suggestion! We appreciate the feedback. Unfortunately, while we could add another method that returns bool indicating whether or not the call has timed out, we have decided not to because it would go against the standard timeout pattern in .NET of throwing TimeoutException. Therefore, I'm going to go ahead and close this bug as Won't Fix.

The good news is that you can easily work around this without having to wait for a fix in the framework itself. You may find the following extension method useful:

    public static class ServiceControllerExtensions {
        public static bool TryWaitForStatus(this ServiceController serviceController, ServiceControllerStatus desiredStatus, TimeSpan timeout) {
            if (!Enum.IsDefined(typeof(ServiceControllerStatus), desiredStatus)) {
                throw new InvalidEnumArgumentException("desiredStatus", (int)desiredStatus, typeof(ServiceControllerStatus));
            }

            try {
                DateTime utcNow = DateTime.UtcNow;
                serviceController.Refresh();

                while (serviceController.Status != desiredStatus) {
                    if ((DateTime.UtcNow - utcNow) > timeout) {
                        return false;
                    }
                    Thread.Sleep(250);
                    serviceController.Refresh();
                }

                return true;
            }
            catch (ObjectDisposedException) {
                return false;
            }
        }
    }

Please let me know if you have any questions or concerns by emailing me at justinv at microsoft dot com.

Regards,

Justin Van Patten
Program Manager
Common Language Runtime, Base Class Libraries
Posted by Microsoft on 5/4/2009 at 12:23 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)