Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / December 2007

Tip: Looking for answers? Try searching our database.

System.ServiceProcess.ServiceController weirdness

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark - 13 Dec 2007 21:42 GMT
Hi...

I inherited some code where someone wanted  to start/stop services and
display a little ... progress bar while waiting.

Originally it was implemented with

ServiceController.{Start|Stop}()
for (int i = 0; i < 60; i++)
{
 Console.WriteLine (".");
 try
 {
   ServiceController.WaitForStatus (desiredState, TimeSpan.FromSeconds(1));
 }
 catch
 {//ignore}
 if (ServiceController.Status == desiredState) break;
}

Knowing how expensive exception processing is, I tried replacing it with
if (ServiceController.Status == desiredState) break;
Thread.Sleep(1000);

I'm finding, however, that the direct comparison is never reached no matter
how long I wait.  Unless WaitForStatus is called (and the exception that
comes with it, usually 4 or 5 seconds/exceptions), the comparison doesn't
work.

I didn't find anything in the docs about this; is this just a known oddity
of the ServiceController?  Seems like WaitForStatus could at least have been
friendlier and returned some kind of boolean success rather than throw an
exception - like WaitForMultipleObjects in the old days.

thanks
Mark
Walter Wang [MSFT] - 14 Dec 2007 06:45 GMT
Hi Mark,

If you use Reflector (http://www.aisto.com/roeder/dotnet/) to view the
ServiceController's implementation, you will see the WaitForStatus() is
also calling Refresh() while checking the status:

public void WaitForStatus(ServiceControllerStatus desiredStatus, TimeSpan
timeout)
{
   if (!Enum.IsDefined(typeof(ServiceControllerStatus), desiredStatus))
   {
       throw new InvalidEnumArgumentException("desiredStatus", (int)
desiredStatus, typeof(ServiceControllerStatus));
   }
   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();
   }
}

The Refresh() is a public method.

I understand your concern here is that the exception is expensive; however,
from the timing aspect of this issue, as we're already waiting using sleep,
this is actually might not be a very big issue.

Regards,
Walter Wang (wawang@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Mark - 14 Dec 2007 14:26 GMT
Thanks, Walter...  That was just what I needed...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.