Click here to Skip to main content
13,803,919 members
Click here to Skip to main content
Add your own
alternative version


64 bookmarked
Posted 2 Jun 2010
Licenced CPOL

Base Library for Multi-threaded Windows Services

, 2 Jun 2010
Rate this:
Please Sign up or sign in to vote.
This is a base library for multi-threaded Windows Services. It manages starting and stopping services, and defines the base structure.


This is a base library for multi-threaded Windows Services. It manages starting and stopping services and defines a base structure.

Library Description



There are three interfaces in the library:

  • IConfigurable defines the interface for the classes which can be configured.
  • IRunnable defines the interface for classes that have a determined lifetime for execution.
  • IService defines the interface for service classes.

ServiceManager Class


This class is the main class in the library, and manages the starting and stopping of services configured in the system. This class implements the Singleton Design Pattern.

ServiceSkeleton Class


This is the base of all service classes. All service classes must implement this class.

How it Works

The entry point is the ServiceManager class.

The first step is defining the service classes to the ServiceManager.

ServiceManager.SingleInstance.Services = new List<IService>
    new WeatherService(),
    new CurrencyService(),
    new BackupService(),
    new StatusChecker()

The second step is signaling ServiceManager to begin the configuration process.


The third step is signaling ServiceManager to start the configured services.


Designing a Service Class

All service classes must implement the ServiceSkeleton class.

public class WeatherService : ServiceSkeleton

After this, three abstract methods must be implemented too.

  • SleepTime
  • ExecuteInternal
  • ConfigureInternal

When execution time comes, the skeleton class calls the ExecuteInternal method. After work is done, the skeleton class sleeps for SleepTime seconds.

For example, a service which gets currency from a website:

using System;
using System.Net;
using ServiceManagerBase;

namespace MyCompany.MyProject.Services
    public class CurrencyService : ServiceSkeleton
        private string _currencyUrl;

        #region Overrides of ServiceSkeleton

        public override int SleepTime()
            return 60*5; // sleep for 5 minutes

        public override void ExecuteInternal()
                Log.Debug(String.Format("Connecting to web site: {0}", _currencyUrl));

                var request = WebRequest.Create(new Uri(_currencyUrl));

            catch (Exception exception)
                Log.Error("Connection failed.", exception);

            // TODO: Rest of the code goes here...

        public override void ConfigureInternal()
            _currencyUrl = "";

            IsConfigured = true;


How to Define a Schedule?

This library is not designed for a schedule operation. But you can schedule jobs. For example:

public override void ExecuteInternal()
    // If time is not 02:00:00, exit

    Log.Debug("Checking if time is 02:00:00");

    if (!(DateTime.Now.Hour == 2 && DateTime.Now.Minute == 0 
                                 && DateTime.Now.Second == 0))
        Log.Debug("Time is not 02:00:00. Exiting.");

    Log.Debug("Time 02:00:00. Starting backup job...");

    // TODO: Do backup job...

Stopping the Service: What to do in Service Classes?

When a service is being stopped, the ServiceManager flags the IsStopRequested boolean field to true. In your code, for example, if you are looping a huge array, you have to control this flag so that the service can stop successfully without error.

Let's say that we have a collection.

var users = GetQueue();

In your code, you loop in this collection. Meanwhile, the service is being stopped. You may choose two of these methods:

In the first method, you handle exiting from the loop process.

foreach (var user in users)
    if (ServiceManager.SingleInstance.IsStopRequested)

    Log.Debug(String.Format("Analysing user ID: {0}", user.UserID));

    // TODO: Analyse...

In the second method, if you're using complex algorithms and if it'll be hard to handle, you can throw an exception.

foreach (var user in users)
    if (ServiceManager.SingleInstance.IsStopRequested)
        throw new StopRequestedException();

    Log.Debug(String.Format("Analysing user ID: {0}", user.UserID));

    // TODO: Analyse...

This exception will be caught by the ServiceSkeleton without any problems.

How to Change the Name of the Log File?

By overriding the GetLoggerName method, you can change the name of the log file.

protected override string GetLoggerName()
    return "Custom logger name";

Base Loop

The base loop in the ServiceSkeleton class is the main loop for every service. It signals the service to execute its own process, manages sleeping, and handles the exceptions.

public void Execute()
    Log.Debug(String.Format("Service \"{0}\" entered the execute loop.", this));

    while (!ServiceManager.SingleInstance.IsStopRequested)
        catch (ThreadAbortException) {}
        catch (StopRequestedException) {}
        catch (Exception ex)
            Log.Error("Error on internal executing process.", ex);

        Log.Debug(String.Format("Sleeping for {0:n0} second(s).", SleepTime()));

        for (var i = 0; i < SleepTime(); i++)

            if (ServiceManager.SingleInstance.IsStopRequested)

    Log.Debug(String.Format("Service \"{0}\" exited from the execute loop.", this));


This is the first published version.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Efe Erdoğru
Software Developer (Senior)
Turkey Turkey
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThread Safety Pin
MSDEVTECHNIK24-Sep-15 5:55
memberMSDEVTECHNIK24-Sep-15 5:55 
QuestionHow can I restart a managed 'child' service if it stops unexpectedly? Pin
jroughgarden22-Jun-12 15:16
memberjroughgarden22-Jun-12 15:16 
AnswerRe: How can I restart a managed 'child' service if it stops unexpectedly? Pin
Efe Erdoğru19-Mar-13 12:24
professionalEfe Erdoğru19-Mar-13 12:24 
GeneralMy vote is definitely 5 too!!! Pin
evrenyagmur11-Feb-11 2:18
memberevrenyagmur11-Feb-11 2:18 
GeneralMy vote is 5 too Pin
Tefik Becirovic6-Sep-10 1:01
memberTefik Becirovic6-Sep-10 1:01 
GeneralRe: My vote is 5 too Pin
Efe Erdoğru6-Sep-10 2:29
professionalEfe Erdoğru6-Sep-10 2:29 
JokeRe: My vote is 5 too Pin
Tefik Becirovic6-Sep-10 2:48
memberTefik Becirovic6-Sep-10 2:48 
GeneralMy vote of 5 Pin
Sandeep Mewara28-Aug-10 8:13
mentorSandeep Mewara28-Aug-10 8:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.181218.1 | Last Updated 2 Jun 2010
Article Copyright 2010 by Efe Erdoğru
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid