By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,529 Members | 1,826 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,529 IT Pros & Developers. It's quick & easy.

stopping windows service.

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a


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 discussion thread is closed

Replies have been disabled for this discussion.