Self terminating windows service

S

SP

Hi All,

I wrote a windows service which is supposed to stop after specified amount
of time. I am calling OnStop() after specified time. OnStop() methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 
N

Nicholas Paldino [.NET/C# MVP]

SP,

I don't think that this is a good idea, since the idea of a service is
that it is supposed to be always running.

If you have a need for a process to run at a certain time, then I would
recommend creating an executable and setting it up as a scheduled task which
runs at a certain time, and then will shut itself down after being run for a
certain amount of time.

If you must use a service, then use the ServiceController class to get
the service and shut it down.

Hope this helps.
 
S

SP

Hi Nicholas,

Thank you for your reply. Let me give a brief background about what I am
trying to achieve.

As a part of framework I am working on, I have a console application and a
Windows service. Console application uses windows service. During execution
console application calls windows service multiple times. I used
ServiceController to start and stop. This part is running fine.

The issue is, I made windows service a stand alone components which can be
executed from services console. Now the task of this service is to generate
a csv file and keep on adding records to it till it is stopped. So just to
make sure that windows service does not keeps on adding records and exhaust
all hard disk space because of human error, I wanted to limit the number of
lines added by the service. Once the specified number of lines are added, I
want to terminate the service. As of now I am limiting the number of lines.
But the service itself is not stopping.

Please let me know whether there is anyway we can do that.

Thanks,

SP

Nicholas Paldino said:
SP,

I don't think that this is a good idea, since the idea of a service is
that it is supposed to be always running.

If you have a need for a process to run at a certain time, then I would
recommend creating an executable and setting it up as a scheduled task which
runs at a certain time, and then will shut itself down after being run for a
certain amount of time.

If you must use a service, then use the ServiceController class to get
the service and shut it down.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


SP said:
Hi All,

I wrote a windows service which is supposed to stop after specified amount
of time. I am calling OnStop() after specified time. OnStop() methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 
N

Nicholas Paldino [.NET/C# MVP]

SP,

It sounds like you basically want shared access to a file from multiple
sources, right?

A service IMO is massive overkill for this.

Rather, you should use a Mutex in a class to block access to other
processes trying to write to the file.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Hi Nicholas,

Thank you for your reply. Let me give a brief background about what I am
trying to achieve.

As a part of framework I am working on, I have a console application and a
Windows service. Console application uses windows service. During
execution
console application calls windows service multiple times. I used
ServiceController to start and stop. This part is running fine.

The issue is, I made windows service a stand alone components which can be
executed from services console. Now the task of this service is to
generate
a csv file and keep on adding records to it till it is stopped. So just to
make sure that windows service does not keeps on adding records and
exhaust
all hard disk space because of human error, I wanted to limit the number
of
lines added by the service. Once the specified number of lines are added,
I
want to terminate the service. As of now I am limiting the number of
lines.
But the service itself is not stopping.

Please let me know whether there is anyway we can do that.

Thanks,

SP

in
message news:[email protected]...
SP,

I don't think that this is a good idea, since the idea of a service
is
that it is supposed to be always running.

If you have a need for a process to run at a certain time, then I would
recommend creating an executable and setting it up as a scheduled task which
runs at a certain time, and then will shut itself down after being run
for a
certain amount of time.

If you must use a service, then use the ServiceController class to
get
the service and shut it down.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


SP said:
Hi All,

I wrote a windows service which is supposed to stop after specified amount
of time. I am calling OnStop() after specified time. OnStop() methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 
S

SP

Nicholas,

No, I am not looking for shared access. I am using this windows service to
read performance counters. The framework is utitlized for testing some
applications and at that time I want to check the performance counters. I
used a service for this task. Users have an option of starting and stopping
the service with out using console applpication. Each time service is
started it creates a new file with what ever performance counters user
wishes to capture.

So I guess there is either no easy way or no way we can have service stop
itself.

Thanks,
Sasidhar.


Nicholas Paldino said:
SP,

It sounds like you basically want shared access to a file from multiple
sources, right?

A service IMO is massive overkill for this.

Rather, you should use a Mutex in a class to block access to other
processes trying to write to the file.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Hi Nicholas,

Thank you for your reply. Let me give a brief background about what I am
trying to achieve.

As a part of framework I am working on, I have a console application and a
Windows service. Console application uses windows service. During
execution
console application calls windows service multiple times. I used
ServiceController to start and stop. This part is running fine.

The issue is, I made windows service a stand alone components which can be
executed from services console. Now the task of this service is to
generate
a csv file and keep on adding records to it till it is stopped. So just to
make sure that windows service does not keeps on adding records and
exhaust
all hard disk space because of human error, I wanted to limit the number
of
lines added by the service. Once the specified number of lines are added,
I
want to terminate the service. As of now I am limiting the number of
lines.
But the service itself is not stopping.

Please let me know whether there is anyway we can do that.

Thanks,

SP

in
message news:[email protected]...
SP,

I don't think that this is a good idea, since the idea of a service
is
that it is supposed to be always running.

If you have a need for a process to run at a certain time, then I would
recommend creating an executable and setting it up as a scheduled task which
runs at a certain time, and then will shut itself down after being run
for a
certain amount of time.

If you must use a service, then use the ServiceController class to
get
the service and shut it down.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi All,

I wrote a windows service which is supposed to stop after specified amount
of time. I am calling OnStop() after specified time. OnStop() methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 
N

Nicholas Paldino [.NET/C# MVP]

SP,

Once again, I have to say this is overkill. If you want to get
performance counter information, why not just use the PerformanceCounter
class in your console application, and have it write the file?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Nicholas,

No, I am not looking for shared access. I am using this windows service to
read performance counters. The framework is utitlized for testing some
applications and at that time I want to check the performance counters. I
used a service for this task. Users have an option of starting and
stopping
the service with out using console applpication. Each time service is
started it creates a new file with what ever performance counters user
wishes to capture.

So I guess there is either no easy way or no way we can have service stop
itself.

Thanks,
Sasidhar.


in
message news:[email protected]...
SP,

It sounds like you basically want shared access to a file from multiple
sources, right?

A service IMO is massive overkill for this.

Rather, you should use a Mutex in a class to block access to other
processes trying to write to the file.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Hi Nicholas,

Thank you for your reply. Let me give a brief background about what I
am
trying to achieve.

As a part of framework I am working on, I have a console application
and a
Windows service. Console application uses windows service. During
execution
console application calls windows service multiple times. I used
ServiceController to start and stop. This part is running fine.

The issue is, I made windows service a stand alone components which can be
executed from services console. Now the task of this service is to
generate
a csv file and keep on adding records to it till it is stopped. So just to
make sure that windows service does not keeps on adding records and
exhaust
all hard disk space because of human error, I wanted to limit the
number
of
lines added by the service. Once the specified number of lines are added,
I
want to terminate the service. As of now I am limiting the number of
lines.
But the service itself is not stopping.

Please let me know whether there is anyway we can do that.

Thanks,

SP

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message SP,

I don't think that this is a good idea, since the idea of a
service
is
that it is supposed to be always running.

If you have a need for a process to run at a certain time, then I
would
recommend creating an executable and setting it up as a scheduled task
which
runs at a certain time, and then will shut itself down after being run
for
a
certain amount of time.

If you must use a service, then use the ServiceController class to
get
the service and shut it down.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi All,

I wrote a windows service which is supposed to stop after specified
amount
of time. I am calling OnStop() after specified time. OnStop()
methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 
S

SP

Nicholas,

Thank you. I found this link online.

http://dotnetjunkies.com/WebLog/jdixon/archive/2004/09/27/26931.aspx

SP


SP said:
Nicholas,

No, I am not looking for shared access. I am using this windows service to
read performance counters. The framework is utitlized for testing some
applications and at that time I want to check the performance counters. I
used a service for this task. Users have an option of starting and stopping
the service with out using console applpication. Each time service is
started it creates a new file with what ever performance counters user
wishes to capture.

So I guess there is either no easy way or no way we can have service stop
itself.

Thanks,
Sasidhar.


message news:[email protected]...
SP,

It sounds like you basically want shared access to a file from multiple
sources, right?

A service IMO is massive overkill for this.

Rather, you should use a Mutex in a class to block access to other
processes trying to write to the file.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Hi Nicholas,

Thank you for your reply. Let me give a brief background about what I am
trying to achieve.

As a part of framework I am working on, I have a console application
and
can
just
to
make sure that windows service does not keeps on adding records and
exhaust
all hard disk space because of human error, I wanted to limit the number
of
lines added by the service. Once the specified number of lines are added,
I
want to terminate the service. As of now I am limiting the number of
lines.
But the service itself is not stopping.

Please let me know whether there is anyway we can do that.

Thanks,

SP

in
message SP,

I don't think that this is a good idea, since the idea of a service
is
that it is supposed to be always running.

If you have a need for a process to run at a certain time, then I
would
recommend creating an executable and setting it up as a scheduled task
which
runs at a certain time, and then will shut itself down after being run
for
a
certain amount of time.

If you must use a service, then use the ServiceController class to
get
the service and shut it down.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi All,

I wrote a windows service which is supposed to stop after specified
amount
of time. I am calling OnStop() after specified time. OnStop() methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 
S

SP

Nicholas,

I haven't done that much research before implementing this solution. But
here are my observations.

I implemented both a console app and service. Both achieve the same. While I
see that windows service is using few of my system resources, console
application is consuming more processing power. I dont want my performance
counter reader to consume more resources. I dont know whether my argument
makes that much sense or not but I am sure about my observation that console
app is consuming more resources for this specific application..

Thanks,
SP



Nicholas Paldino said:
SP,

Once again, I have to say this is overkill. If you want to get
performance counter information, why not just use the PerformanceCounter
class in your console application, and have it write the file?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Nicholas,

No, I am not looking for shared access. I am using this windows service to
read performance counters. The framework is utitlized for testing some
applications and at that time I want to check the performance counters. I
used a service for this task. Users have an option of starting and
stopping
the service with out using console applpication. Each time service is
started it creates a new file with what ever performance counters user
wishes to capture.

So I guess there is either no easy way or no way we can have service stop
itself.

Thanks,
Sasidhar.


in
message news:[email protected]...
SP,

It sounds like you basically want shared access to a file from multiple
sources, right?

A service IMO is massive overkill for this.

Rather, you should use a Mutex in a class to block access to other
processes trying to write to the file.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi Nicholas,

Thank you for your reply. Let me give a brief background about what I
am
trying to achieve.

As a part of framework I am working on, I have a console application
and a
Windows service. Console application uses windows service. During
execution
console application calls windows service multiple times. I used
ServiceController to start and stop. This part is running fine.

The issue is, I made windows service a stand alone components which
can
be
executed from services console. Now the task of this service is to
generate
a csv file and keep on adding records to it till it is stopped. So
just
to
make sure that windows service does not keeps on adding records and
exhaust
all hard disk space because of human error, I wanted to limit the
number
of
lines added by the service. Once the specified number of lines are added,
I
want to terminate the service. As of now I am limiting the number of
lines.
But the service itself is not stopping.

Please let me know whether there is anyway we can do that.

Thanks,

SP

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message SP,

I don't think that this is a good idea, since the idea of a
service
is
that it is supposed to be always running.

If you have a need for a process to run at a certain time, then I
would
recommend creating an executable and setting it up as a scheduled task
which
runs at a certain time, and then will shut itself down after being run
for
a
certain amount of time.

If you must use a service, then use the ServiceController class to
get
the service and shut it down.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi All,

I wrote a windows service which is supposed to stop after specified
amount
of time. I am calling OnStop() after specified time. OnStop()
methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 
N

Nicholas Paldino [.NET/C# MVP]

SP,

Lets assume that reading the performance counters has a cost of X. Now
running a console app has a cost of Y, and running a service has a cost of
Z. When I say running a console app or service, I mean running just the
console, or the service, not the performance counter in the service.

So now, if you do it all in the console app, you have a cost of X + Y.
If you do it in the service, and have the console app activate the service,
you have a cost of X + Y + Z.

This is an oversimplification, I admit.

In reality, reading the performance counters in a service versus a
console app are going to both take up the same amount of processing power,
since they are doing the same thing. Just because you take the code that is
consuming resources and putting it somewhere else doesn't mean that it isn't
paying the price for performing those operations.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Nicholas,

I haven't done that much research before implementing this solution. But
here are my observations.

I implemented both a console app and service. Both achieve the same. While
I
see that windows service is using few of my system resources, console
application is consuming more processing power. I dont want my performance
counter reader to consume more resources. I dont know whether my argument
makes that much sense or not but I am sure about my observation that
console
app is consuming more resources for this specific application..

Thanks,
SP



in
message news:[email protected]...
SP,

Once again, I have to say this is overkill. If you want to get
performance counter information, why not just use the PerformanceCounter
class in your console application, and have it write the file?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Nicholas,

No, I am not looking for shared access. I am using this windows service to
read performance counters. The framework is utitlized for testing some
applications and at that time I want to check the performance counters. I
used a service for this task. Users have an option of starting and
stopping
the service with out using console applpication. Each time service is
started it creates a new file with what ever performance counters user
wishes to capture.

So I guess there is either no easy way or no way we can have service stop
itself.

Thanks,
Sasidhar.


"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message SP,

It sounds like you basically want shared access to a file from
multiple
sources, right?

A service IMO is massive overkill for this.

Rather, you should use a Mutex in a class to block access to other
processes trying to write to the file.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi Nicholas,

Thank you for your reply. Let me give a brief background about what
I
am
trying to achieve.

As a part of framework I am working on, I have a console application
and
a
Windows service. Console application uses windows service. During
execution
console application calls windows service multiple times. I used
ServiceController to start and stop. This part is running fine.

The issue is, I made windows service a stand alone components which can
be
executed from services console. Now the task of this service is to
generate
a csv file and keep on adding records to it till it is stopped. So just
to
make sure that windows service does not keeps on adding records and
exhaust
all hard disk space because of human error, I wanted to limit the
number
of
lines added by the service. Once the specified number of lines are
added,
I
want to terminate the service. As of now I am limiting the number of
lines.
But the service itself is not stopping.

Please let me know whether there is anyway we can do that.

Thanks,

SP

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message SP,

I don't think that this is a good idea, since the idea of a
service
is
that it is supposed to be always running.

If you have a need for a process to run at a certain time, then I
would
recommend creating an executable and setting it up as a scheduled task
which
runs at a certain time, and then will shut itself down after being run
for
a
certain amount of time.

If you must use a service, then use the ServiceController class to
get
the service and shut it down.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi All,

I wrote a windows service which is supposed to stop after specified
amount
of time. I am calling OnStop() after specified time. OnStop()
methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 
S

SP

I am not quite sure about how console applications and windows services are
handled by .NET framework. Putting everything into a console application has
few drawbacks. You will need to do thread processing because my console
application also does something other than starting and stopping the
service. The other thing is.. you cant control the program independently.
When I implemented thread processing my application was using lot of
resources. Using this approach my application never exceeded 2% CPU
utilization.

I used to believe in this
"Just because you take the code that is consuming resources and putting it
somewhere else doesn't mean that it isn't paying the price for performing
those operations."

But now I am not sure now... May be my application architecture was bad when
I had multi-thread solution.

My idea of separating is also driven by the thought of having multiple
independent services which can be reused. Influenced by SOA.

Thanks,
SP



Nicholas Paldino said:
SP,

Lets assume that reading the performance counters has a cost of X. Now
running a console app has a cost of Y, and running a service has a cost of
Z. When I say running a console app or service, I mean running just the
console, or the service, not the performance counter in the service.

So now, if you do it all in the console app, you have a cost of X + Y.
If you do it in the service, and have the console app activate the service,
you have a cost of X + Y + Z.

This is an oversimplification, I admit.

In reality, reading the performance counters in a service versus a
console app are going to both take up the same amount of processing power,
since they are doing the same thing. Just because you take the code that is
consuming resources and putting it somewhere else doesn't mean that it isn't
paying the price for performing those operations.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SP said:
Nicholas,

I haven't done that much research before implementing this solution. But
here are my observations.

I implemented both a console app and service. Both achieve the same. While
I
see that windows service is using few of my system resources, console
application is consuming more processing power. I dont want my performance
counter reader to consume more resources. I dont know whether my argument
makes that much sense or not but I am sure about my observation that
console
app is consuming more resources for this specific application..

Thanks,
SP



in
message news:[email protected]...
SP,

Once again, I have to say this is overkill. If you want to get
performance counter information, why not just use the PerformanceCounter
class in your console application, and have it write the file?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas,

No, I am not looking for shared access. I am using this windows
service
to
read performance counters. The framework is utitlized for testing some
applications and at that time I want to check the performance
counters.
I
used a service for this task. Users have an option of starting and
stopping
the service with out using console applpication. Each time service is
started it creates a new file with what ever performance counters user
wishes to capture.

So I guess there is either no easy way or no way we can have service stop
itself.

Thanks,
Sasidhar.


"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message SP,

It sounds like you basically want shared access to a file from
multiple
sources, right?

A service IMO is massive overkill for this.

Rather, you should use a Mutex in a class to block access to other
processes trying to write to the file.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi Nicholas,

Thank you for your reply. Let me give a brief background about what
I
am
trying to achieve.

As a part of framework I am working on, I have a console application
and
a
Windows service. Console application uses windows service. During
execution
console application calls windows service multiple times. I used
ServiceController to start and stop. This part is running fine.

The issue is, I made windows service a stand alone components
which
can
be
executed from services console. Now the task of this service is to
generate
a csv file and keep on adding records to it till it is stopped. So just
to
make sure that windows service does not keeps on adding records and
exhaust
all hard disk space because of human error, I wanted to limit the
number
of
lines added by the service. Once the specified number of lines are
added,
I
want to terminate the service. As of now I am limiting the number of
lines.
But the service itself is not stopping.

Please let me know whether there is anyway we can do that.

Thanks,

SP

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message SP,

I don't think that this is a good idea, since the idea of a
service
is
that it is supposed to be always running.

If you have a need for a process to run at a certain time,
then
I
would
recommend creating an executable and setting it up as a scheduled task
which
runs at a certain time, and then will shut itself down after
being
run
for
a
certain amount of time.

If you must use a service, then use the ServiceController
class
to
get
the service and shut it down.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi All,

I wrote a windows service which is supposed to stop after specified
amount
of time. I am calling OnStop() after specified time. OnStop()
methods
executed but I dont see the service stopping.

Please advise how to stop the service.

Thanks,
SP
 

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