How Windows Service can shutdown itself?

M

Mika M

Hi,

I'm working for Windows Service which need to do short basic task only
once per day. I think to start it using Scheduled Tasks, and propably
Scheduled Task will run just bat-file to start the service - or better
suggestions?

Then when service has done the basic task, it should shutdown by itself
(to save computer resources) - until Scheduled Task starts it again
(next day).

Question: How to make C# 2005 Windows Service to shultdown itself?
 
P

Pavel Minaev

Mika M said:
Hi,

I'm working for Windows Service which need to do short basic task only
once per day. I think to start it using Scheduled Tasks, and propably
Scheduled Task will run just bat-file to start the service - or better
suggestions?

If you want to do it that way, then what you write is not a service. A
service is, by definition, a process constantly running in the background;
it's not something that is scheduled. A service could, of course, schedule
to run a thread within it once a day, and you might want to consider doing
so. Otherwise, why would you want a service at all? A plain console
application would do just fine.
 
M

Mika M

OK good points. Have to rethink how to do this. What I need to do is to
make SQL Server 2005 database SELECT-query, and save the result as
xml-file into certain folder for another application (ERP), which will
import it from that folder. This have to run just once per day. Suggestion?
 
D

Duggi

Hi,

I'm working for Windows Service which need to do short basic task only
once per day. I think to start it using Scheduled Tasks, and propably
Scheduled Task will run just bat-file to start the service - or better
suggestions?

Then when service has done the basic task, it should shutdown by itself
(to save computer resources) - until Scheduled Task starts it again
(next day).

Question: How to make C# 2005 Windows Service to shultdown itself?

A service is something that should be available always...

I think in your case you should probably replace the service design
with a demon application, which will be directly stared by the
scheduled tasks

-Cnu
 
D

Duggi

OK good points. Have to rethink how to do this. What I need to do is to
make SQL Server 2005 database SELECT-query, and save the result as
xml-file into certain folder for another application (ERP), which will
import it from that folder. This have to run just once per day. Suggestion?






- Show quoted text -

My suggestion is...

1. Write a console program, which will execute the SQL query and
stores the result in XML file, in whcih ever folder you want.
2. Write a batch file to invoke the console program appropriately (if
you have planned some parameters for the console program). If you do
not have any parameters to the console, you can directly invoke
console application.
3. Create a scheduled task to run the batch file or the console
program.

As the ERP application is going to take the file from the folder, your
workflow is completed.

I think this is helpful for you.


-Cnu
 
P

Pavel Minaev

Mika M said:
OK good points. Have to rethink how to do this. What I need to do is to
make SQL Server 2005 database SELECT-query, and save the result as
xml-file into certain folder for another application (ERP), which will
import it from that folder. This have to run just once per day.
Suggestion?

You might not need to write a distinct program for that at all - consider
using SELECT ... FOR XML and bcp.exe. For example, here's a command that
exports a single table from AdventureWorks sample database as XML:

bcp "SELECT * FROM AdventureWorks.Person.Contact FOR XML AUTO" queryout
contact.txt -T -c

Of course this is the simplest form, normally you'd use FOR XML EXPLICIT to
define the data shape to be what you want; or, possibly, use FOR XML RAW in
the query, and some XSLT to transform it to the desired format, combining it
all in a batch file. And then you can schedule to run that daily by any
usual means available in Windows.
 
P

Pavel Minaev

Pavel Minaev said:
You might not need to write a distinct program for that at all - consider
using SELECT ... FOR XML and bcp.exe. For example, here's a command that
exports a single table from AdventureWorks sample database as XML:

bcp "SELECT * FROM AdventureWorks.Person.Contact FOR XML AUTO" queryout
contact.txt -T -c

I apologize for a mistake; apparently, if you want to get well-formed XML in
the output, the correct syntax is actually:

bcp "SELECT * FROM AdventureWorks.Person.Contact FOR XML AUTO, ROOT"
queryout contact.xml -T -w -r ""

This will ensure that no line breaks happen in the middle of
attribute/element names, and provide a single root element.
 
D

Duggi

I apologize for a mistake; apparently, if you want to get well-formed XMLin
the output, the correct syntax is actually:

  bcp "SELECT * FROM AdventureWorks.Person.Contact FOR XML AUTO, ROOT"
queryout contact.xml -T -w -r ""

This will ensure that no line breaks happen in the middle of
attribute/element names, and provide a single root element.

Excellent!!!! Till now I did not know that there is an utility like
this (BCP.EXE). Thank you Pavel Minaev.

I just read http://msdn.microsoft.com/en-us/library/ms162802.aspx

-Cnu
 
J

JDeats

Hi,

I'm working for Windows Service which need to do short basic task only
once per day. I think to start it using Scheduled Tasks, and propably
Scheduled Task will run just bat-file to start the service - or better
suggestions?

Then when service has done the basic task, it should shutdown by itself
(to save computer resources) - until Scheduled Task starts it again
(next day).

Question: How to make C# 2005 Windows Service to shultdown itself?

I don't entirely agree with the other posters who have tried to steer
you away from a Windows service to solve your problem. Without knowing
exactly what procedures will be executed during your scheduled task
(i.e. it's purpose). Programmaticly adding and removing a service can
be achieved through .NET (see link below), but you'll find many
software vendors continuing to implement their own scheduler inside a
Windows Service that identifies itself to the user. (e.g. Apple's
iTunes/QuickTime update service). If your service exist to facilitate
another application (as would be the case with an update service) it
would probably make more sense to implement this as a Windows Service.
You other applications can easily communicate with that service; stop
or re-start it if need be.

Having said that, you would not have the Windows Service in a stopped
or disabled state. This would be an action the user would be free to
take if they didn't want the service operating for whatever reason and
they would accomplish that task through the Services settings in
Administrator Tools, Control Panel. Inside your service you would have
a TimerThread that executes every x milliseconds. Effectively the
service always runs in the background as its own scheduler. The
advantages this approach has: It's easier for the UI driven app the
service is facilitating to monitor the health of this service (e.g. on
launch, with a few lines of code you can determine if the service is
running and if not re-start) also many Installer applications support
installation and removal of Windows Services to get an application
installed into the scheduler will be a bit more code intensive, also
it's relatively easy to assign (programmaticly) and to reassign
programmability or manually for the user the Windows system account
that the service will be running under..


Reference:
http://www.codeproject.com/KB/cs/tsnewlib.aspx
 
M

Mr. Arnold

Mika M said:
Hi,

I'm working for Windows Service which need to do short basic task only
once per day. I think to start it using Scheduled Tasks, and propably
Scheduled Task will run just bat-file to start the service - or better
suggestions?

Then when service has done the basic task, it should shutdown by itself
(to save computer resources) - until Scheduled Task starts it again (next
day).

Question: How to make C# 2005 Windows Service to shultdown itself?

You can just make a simple Console Application and schedule the exe to run
as Windows NT based O/S scheduled task. I did a Web service client
application that way that started, read SQL Server tables, and it uploaded
the data in XML to a 3rd party Web service. It was started, did its thing,
and it shut itself down. Operations could also run the application exe from
the Command Line too, to do none scheduled uploads, because of corrected bad
XML data being uploaded.
 

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