Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:49:11+00:00 2026-05-26T02:49:11+00:00

Starting with a csharp-example and duly noting related SO questions ( Restart a windows

  • 0

Starting with a csharp-example and duly noting related SO questions ( Restart a windows services from C# and Cannot restart a Service) and various other questions relating to restarting just one service, I’m wondering what the best method is for restarting a service with dependent services (e.g. Message Queuing, on which Message Queuing Triggers depends, or IIS, on which FTP Publishing and World Wide Web Publishing depend). The mmc snap-in does this automagically, but the code doesn’t seem to provide the same functionality (at least not as easily).

MSDN documentation for Stop says “If any services depend on this service for their operation, they will be stopped before this service is stopped. The DependentServices property contains the set of services that depend on this one,” and DependentServices returns an array of services. Assuming StartService() and StopService() follow the conventions outlined in the examples and such referenced above (except that they accept ServiceControllers and TimeSpans directly), I started with:

public static void RestartServiceWithDependents(ServiceController service, TimeSpan timeout)
{
    ServiceController[] dependentServices = service.DependentServices;

    RestartService(service, timeout); // will stop dependent services, see note below* about timeout...

    foreach (ServiceController dependentService in dependentServices)
    {
        StartService(dependentService, timeout);
    }
}

But what if the service dependencies are nested (recursive) or cyclical (if that’s even possible…) – if Service A is depended on by Service B1 and Service B2 and Service C1 depends on Service B1, it seems ‘restarting’ Service A by this method would stop Service C1 but wouldn’t restart it…

To make this example picture clearer, I’ll follow the model in the services mmc snap-in:

The following system components depend on [Service A]:
  - Service B1
    - Service C1
  - Service B2

Is there a better way to go about this, or would it just have to recursively step into and stop each dependent service and then restart them all after it restarts the main service?

Additionally, will dependent but currently stopped services be listed under DependentServices? If so, wouldn’t this restart them anyways? If so, should we control that as well? This just seems to get messier and messier…

*Note: I realize the timeout isn’t being applied completely correctly here (overall timeout could be many many times longer than expected), but for now that’s not the issue I’m concerned about – if you want to fix it, fine, but don’t just say ‘timeout’s broken…’

Update: After some preliminary testing, I’ve discovered (/confirmed) the following behaviors:

  • Stopping a service (e.g. Service A) that other services (e.g. Service B1) depend on will stop the other services (including “nested” dependencies such as Service C1)
  • DependentServices does include dependent services in all states (Running, Stopped, etc.), and it also includes nested dependencies, i.e. Service_A.DependentServices would contain {Service B1, Service C1, Service B2} (in that order, as C1 depends on B1).
  • Starting a service that depends on others (e.g. Service B1 depends on Service A) will also start the requisite services.

The code above can therefore be simplified (at least in part) to just stop the main service (which will stop all dependent services) and then restarting the most-dependent services (e.g. Service C1 and Service B2) (or just restarting “all” the dependent services – it will skip the ones already started), but that really just defers the starting of the main service momentarily until one of the dependencies complain about it, so that doesn’t really help.

Looks for now like just restarting all the dependencies is the simplest way, but that ignores (for now) managing services that are already stopped and such…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:49:11+00:00Added an answer on May 26, 2026 at 2:49 am

    Alright, finally implemented this. I’ve posted it as a separate answer as I had already come to this conclusion in the original update to my question, which was posted prior to the first answer.

    Again, the StartService(), StopService() and RestartService() methods follow the conventions outlined in the examples and such already referenced in the question itself (i.e. they wrap Start/Stop behavior to avoid “already started/stopped”-type exceptions) with the addendum that if a Service is passed in (as is the case below), Refresh() is called on that service before checking its Status.

    public static void RestartServiceWithDependents(ServiceController service, TimeSpan timeout)
    {
        int tickCount1 = Environment.TickCount; // record when the task started
    
        // Get a list of all services that depend on this one (including nested
        //  dependencies)
        ServiceController[] dependentServices = service.DependentServices;
    
        // Restart the base service - will stop dependent services first
        RestartService(service, timeout);
    
        // Restore dependent services to their previous state - works because no
        //  Refresh() has taken place on this collection, so while the dependent
        //  services themselves may have been stopped in the meantime, their
        //  previous state is preserved in the collection.
        foreach (ServiceController dependentService in dependentServices)
        {
            // record when the previous task "ended"
            int tickCount2 = Environment.TickCount;
            // update remaining timeout
            timeout.Subtract(TimeSpan.FromMilliseconds(tickCount2 - tickCount1));
            // update task start time
            tickCount1 = tickCount2;
            switch (dependentService.Status)
            {
                case ServiceControllerStatus.Stopped:
                case ServiceControllerStatus.StopPending:
                    // This Stop/StopPending section isn't really necessary in this
                    //  case as it doesn't *do* anything, but it's included for
                    //  completeness & to make the code easier to understand...
                    break;
                case ServiceControllerStatus.Running:
                case ServiceControllerStatus.StartPending:
                    StartService(dependentService, timeout);
                    break;
                case ServiceControllerStatus.Paused:
                case ServiceControllerStatus.PausePending:
                    StartService(dependentService, timeout);
                    // I don't "wait" here for pause, but you can if you want to...
                    dependentService.Pause();
                    break;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting from ASP.NET MVC Preview 3, HTML.Button ( and other related HTML controls) are
Starting from this example: http://support.microsoft.com/kb/828736 I have tried to add a test function in
Starting from Windows Server 2003, Windows included a new tool which calculates the effective
Starting with the following LINQ query: from a in things where a.Id == b.Id
Starting from scratch with very little knowledge of .NET, how much ASP.NET should I
Starting from an Html input like this: <p> <a href=http://www.foo.com>this if foo</a> <a href=http://www.bar.com>this
Starting from an Html input like this: <p> <a href=http://www.foo.com>this if foo</a> <a href=http://www.bar.com>this
Starting from branch master, to merge a topic branch one just says git merge
Starting using werkzeug, i try to map urls (from a file urls.py) to views
Starting from a totally empty MXML application: <?xml version=1.0 encoding=utf-8?> <mx:Application xmlns:mx=http://www.adobe.com/2006/mxml layout=absolute >

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.