Solved

How to stop a Windows Service gracefully?

Posted on 2007-11-19
3
813 Views
Last Modified: 2013-11-26
I have a Windows Service which processes some files every X seconds. The service should run 24/7 unless there is an exception, in which case I stop the service. However, there are times when I would want to stop the service manually (through Admin Tools/Services). How do I ensure that when I do that while the function that does the file processing is still executing, the service will wait for the function to finish? Is there anything I can put in OnStop() to detect whether the function is running or not? I suppose I could have the function turn some app settings on when it starts executing and off when it finishes executing, and add some code in OnStop() to check the setting. However, I would have to implement a loop to constantly check for the flag, plus I don't know how long OnStop() can wait?

I would appreciate any suggestions. Thank you.
0
Comment
Question by:EwS
  • 2
3 Comments
 
LVL 10

Assisted Solution

by:MrClyfar
MrClyfar earned 100 total points
ID: 20313830
Hi there.

Here's a link to a similar issue:

http://www.thescripts.com/forum/thread124604.html

What you can do is use the thread.Join technique to try and come to a smooth halt.

Hope this help.s
Jas.
0
 
LVL 8

Expert Comment

by:slado2
ID: 20314263
If your service takes some time to stop you should inform servicemanager that the service status is stop pending and you need some more time.
http://msdn2.microsoft.com/en-us/library/d56de412(VS.80).aspx
0
 
LVL 8

Accepted Solution

by:
slado2 earned 150 total points
ID: 20314755
To make things clear:
I assume that your service processing (file operations in your case) is done in a separate thread. When OnStop is called then you should call Join(int Time)
http://msdn2.microsoft.com/en-us/library/6b1kkss0.aspx
The call RequestAdditionalTime to inform the service manager that your service is still stopping if the thread is not finished yet.
http://msdn2.microsoft.com/en-us/library/system.serviceprocess.servicebase.requestadditionaltime.aspx
protected override void OnStop()

{

  int joinTimeout = 500; // 500 ms

  while(!FileProcessingThread.Join(joinTimeout))

  {

    this.RequestAdditionalTime(1000);  // request additional 1 second to stop the service

  }

}

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now