473,387 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

stopping windows service.

Hi all,

I am having one windows service which is updating to database.

On 'Onstop i want to wait till current updation complete. How will i
do this?

Because if i write some lengthy code on onstop it fails to stop and
status remain 'stopping' Later i can't do anything on that service
even i can't uninstall that service.

Please tell me solution for this.

Please help me asap.

thanks in advance.

Jul 30 '07 #1
10 7489
I believe that once you are in OnStop you've invoked the Service Control
Manager and if she can't stop your service in the alloted time, she will blow
up.

You could check if the operation is complete and bail out if it isn't. Other
folks do stuff like this:

private void InvokeOnStop()
{
ServiceController sc = new ServiceController(this.ServiceName);

sc.Stop();
sc.Dispose();
}

-- Although current wisdom indicates that it isn't a good idea.

Peter
--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
bogMetaFinder: http://www.blogmetafinder.com

"archana" wrote:
Hi all,

I am having one windows service which is updating to database.

On 'Onstop i want to wait till current updation complete. How will i
do this?

Because if i write some lengthy code on onstop it fails to stop and
status remain 'stopping' Later i can't do anything on that service
even i can't uninstall that service.

Please tell me solution for this.

Please help me asap.

thanks in advance.

Jul 30 '07 #2
Hi,

thanks for your reply.

But how will i wait for some operation to complete on 'onstop event'.
See like in windows service i can forece user to wait for some
operation to complete by using waitall. That won't cause any
problem. Like that what i need to do in windows service to wait for
current operation to complete.

if u can shed some light on it then it will be really helpful for me,

thanks.

Jul 30 '07 #3
"archana" <tr**************@yahoo.comwrote in message
news:11*********************@d55g2000hsg.googlegro ups.com...
But how will i wait for some operation to complete on 'onstop event'.
See like in windows service i can forece user to wait for some
operation to complete by using waitall. That won't cause any
problem. Like that what i need to do in windows service to wait for
current operation to complete.

if u can shed some light on it then it will be really helpful for me,
I usually set a boolean flag true when a process starts and then set it
false when the process ends.

In the OnStop event, I check the value of the flag and wait until it is
false before shutting down the service.
--
Mark Rae
ASP.NET MVP
http://www.markrae.net

Jul 30 '07 #4

"archana" <tr**************@yahoo.comwrote in message
news:11**********************@d55g2000hsg.googlegr oups.com...
Hi all,

I am having one windows service which is updating to database.

On 'Onstop i want to wait till current updation complete. How will i
do this?

Because if i write some lengthy code on onstop it fails to stop and
status remain 'stopping' Later i can't do anything on that service
even i can't uninstall that service.

Please tell me solution for this.

Please help me asap.
When you execute the code each time for the update, you set a global Boolean
flag at the top of the routine to *true* like blnUpdating and set the flag
to false when the routine is exited.

Then in your OnStop you will have code like this.

While blnUpdating
{
make it wait one second by using System.ThreadTimer.Sleep(wait time)
}

It will stay in the While loop as long as blnUpdating = true.
It will come out of the While loop when blnUpdating is set to false when the
Update routine has exited.

The code in the OnStop will be executed after the loop to stop the service.

The Update routine may need to be running on a child thread while the Onstop
is on the parent thread the thread the service started up on.

You may also want to look at the service's 'CanStop' property by setting it
to true or false as well, but I have never used that property to prevent the
service from stopping.

Jul 30 '07 #5
Hi all,

thanks for replies.

I used same thing. I am getting following error

Could not stop the 'MyTestService' service on Local Computer.

Error 1053: The service did not respond to the start or
control request in a timely fashion.

Can anyone tell what to do to solve this problem. Becase its major
problem and on service i can't restart server everytime if this error
occurs. And i want proper way to stop service.

thanks.

Jul 30 '07 #6
When a service is told to stop, I believe it has 30 seconds before the
Service Control Manager abandons the service.

What you need to do is call the SetServiceStatus API function through
the P/Invoke layer, using the value returned from the ServiceHandle property
(it is the handle to the service which you can pass to the SetServiceStatus
function).

In your OnStop method, you would have to wait a little bit, check to see
if the update completed, then call SetServiceStatus, indicating that you are
waiting (incrementing the dwCheckPoint field of the SERVICE_STATUS
structure, so that your service knows you are doing something).

I can't recall completely, but I think there is an absolute value that
windows will wait for the service to wait, even in light of the calls to
SetServiceStatus, which if exceeded, will cause the service to be
terminated, so you might want to make sure your update completes in a timely
manner in this case.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"archana" <tr**************@yahoo.comwrote in message
news:11**********************@l70g2000hse.googlegr oups.com...
Hi all,

thanks for replies.

I used same thing. I am getting following error

Could not stop the 'MyTestService' service on Local Computer.

Error 1053: The service did not respond to the start or
control request in a timely fashion.

Can anyone tell what to do to solve this problem. Becase its major
problem and on service i can't restart server everytime if this error
occurs. And i want proper way to stop service.

thanks.

Jul 30 '07 #7

"archana" <tr**************@yahoo.comwrote in message
news:11**********************@l70g2000hse.googlegr oups.com...
Hi all,

thanks for replies.

I used same thing. I am getting following error

Could not stop the 'MyTestService' service on Local Computer.

Error 1053: The service did not respond to the start or
control request in a timely fashion.

Can anyone tell what to do to solve this problem. Becase its major
problem and on service i can't restart server everytime if this error
occurs. And i want proper way to stop service.
I think the service has blown up somewhere or at some point it went into a
loop somewhere and didn't come out, which in either case it's causing the
Onstop to timeout. You need to debug your service.

Jul 30 '07 #8
Hi,

thanks for your reply.

can u tell me how to do this. Because i searched on net to get
information about it to implement in framework 1.1. But of no luck.

please help me.

thanks

Jul 31 '07 #9
I kind of did already in my previous post? The SetServiceStatus API is
a windows API function, and you have to call it through the P/Invoke layer.
You can get the definition from http://www.pinvoke.net most likely. You can
also most likely get the definition of the SERVICE_STATUS structure from
there as well.

The ServiceHandle property is not available in .NET 1.1, so you will
have to resort to reflection on the ServiceBase instance (the class you
derive from) to get the value. It would be an implementation detail, but
when you migrate beyond 1.1, you know it will be supported (it's a bit of a
hack, at the least in 1.1, but from what I can tell, the only way to get
what you want).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"archana" <tr**************@yahoo.comwrote in message
news:11**********************@w3g2000hsg.googlegro ups.com...
Hi,

thanks for your reply.

can u tell me how to do this. Because i searched on net to get
information about it to implement in framework 1.1. But of no luck.

please help me.

thanks

Jul 31 '07 #10


Hi archana,

I saw ur thread regarding stopping the windows service.

Even I am facing the same problem.

I have created a thread in the OnStart() event of the windows service
and calling the method of the class library project(.dll).
I put the thread to sleep in the method for some period of time.(1 min)
On the OnStop() event I want to stop the service only after completion
of the work by the thread.(not in the middle.)

Did you get any solution for your problem?
If yes can you please share with me that or provide me the sample code
for the same?

Thanx in advance.

*** Sent via Developersdex http://www.developersdex.com ***
Aug 8 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: CG | last post by:
Hi I have developed a Windows Service When I try to start the Service it tells me that it cannot start as there may not be any work to do When I comment out below line of code in my OnStart...
0
by: Daniel O'Brien | last post by:
Hi - any help with this would be greatly appreicated - it has already had me confused for a good few hours! I am using Visual Studio 2003 and the .NET framework 1.1. I have a C# Windows...
4
by: Keith | last post by:
I'm in the same boat as the fellow who posted this message back in August: Title : Windows Service, How does one make a service "fail" properly? Author : Ross Bennett Group :...
0
by: GTS | last post by:
Hi, I have created my application to run as windows service. It is developed using VC++ on Windows 2000 platform. It is working fine with Windows 2000. But I am seeing problems in Windows 2003...
7
by: Gene | last post by:
I have a Windows Service (VB.NET) that runs 24/7. It queries a Web service 5 to 10 times per hour. About 2 or 3 times a month, it fails. The log indicates that it sends the request to the Web...
0
by: news.microsoft.com | last post by:
Hi, I have a windows service that stops, but in the control panel/services it says that it is running. When I try to stop it, it times out, and just says 'stopping' as its state. I have to kill...
0
by: Glen Wolinsky | last post by:
I am creating a Windows service that will check a request queue (database) for pending requests. It will then process each individual request until completed, wait a set time interval and then...
5
by: chris.hearson | last post by:
How do I programmatically prevent a service from stopping? I want to be able to keep my service running (started), under certain conditions, when a user tries to stop it. I have tried throwing an...
6
by: D | last post by:
I have a simple file server utility that I wish to configure as a Windows service - using the examples of the Python Win32 book, I configured a class for the service, along with the main class...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.