stopping windows service.

A

archana

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.
 
G

Guest

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
 
A

archana

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.
 
M

Mark Rae [MVP]

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.
 
M

Mr. Arnold

archana said:
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.
 
A

archana

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
M

Mr. Arnold

archana said:
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.
 
A

archana

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
 
N

Nicholas Paldino [.NET/C# MVP]

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).
 
S

Shilpa Ginode

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top