1

I have a web service which I need to ensure will finish processing then exit when onstop() is called. Currently when onstop() is called the service immediately stops. I've been told to look at ManualResetEvent and a requeststop flag. I've looked everywhere for examples even found a few of these:

How do I safely stop a C# .NET thread running in a Windows service?

To make a choice between ManualResetEvent or Thread.Sleep()

http://www.codeproject.com/Articles/19370/Windows-Services-Made-Simple

But I've having so much trouble understanding which one I can best apply to my situation.

Code below:

        System.Timers.Timer timer = new System.Timers.Timer();
        private volatile bool _requestStop = false;
        private static readonly string connStr = ConfigurationManager.ConnectionStrings["bedbankstandssConnectionString"].ConnectionString;
        private readonly ManualResetEvent _allDoneEvt = new ManualResetEvent(true);
        public InvService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            _requestStop = false;
            timer.Elapsed += timer_Elapsed;
            double pollingInterval = Convert.ToDouble(ConfigurationManager.AppSettings["PollingInterval"]);
            timer.Interval = pollingInterval;
            timer.Enabled = true;
            timer.Start();      
        }

        protected override void OnStop()
        {
            _requestStop = true;
            timer.Dispose();
        }

        protected override void OnContinue()
        { }

        protected override void OnPause()
        { }

        private void timer_Elapsed(object sender, EventArgs e)
        {
            if (!_requestStop)
            {
                timer.Start(); 
                InvProcessingChanges();     
            }               
        }

        private void InvProcessingChanges()
        { 
           //Processes changes to inventory
        }

Is anybody experienced with windows services there who can help me? I'm quite new to windows services having just completed my first working service. This service needs to complete sending inventory updates before it actually stops.

Community
  • 1
  • 1
user1270384
  • 711
  • 7
  • 24
  • 53

1 Answers1

3

You an use something like ManualResetEvent to wait until the event enters a signalled state before completing Stop. ManualResetEventSlim might be more appropriate considering you're trying to signal within the same process.

Basically, you can call Wait on the event during the stop and while you're processing call Reset and when you're done, call Set.

e.g.

private ManualResetEventSlim resetEvent = new ManualResetEventSlim(false);

public void InvProcessingChanges()
{
    resetEventSlim.Reset();
    try
    {
        // TODO: *the* processing
    }
    finally
    {
        resetEvent.Set();
    }
}

public void WaitUntilProcessingComplete()
{
    resetEvent.Wait();
}

and depending your your service:

    protected override void OnStop()
    {
        WaitUntilProcessingComplete();
    }
Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • Just a question @peter is your WebServiceProcessMethod my invprocessingchanges? – user1270384 Feb 19 '13 at 20:49
  • Note how my Onstop has the following code protected override void OnStop() { _requestStop = true; timer.Dispose(); } does it now change or is your code to be included with this? – user1270384 Feb 19 '13 at 22:26
  • Yes, you want to keep the timer.Dispose in there, otherwise you might get another Elapsed event while the service is shutting down – Peter Ritchie Feb 19 '13 at 22:29