views:

3225

answers:

7

Hi, I am about to develop a console application that will be required to continually run and carry out work at specific times.

My question is what is are best methods or practices to keep your application alive?

My thought were: A loop that never ends? A timer that sleeps and then jumps to routine when required (after set sleep period)?

I will be compiling the application into an exe and then running it as a service using AlwaysUp.

Regards..

Peter

+6  A: 

Why don't you build your app as a service in the first place?

Chris W. Rea
WIndows should use services as that is the model it best supports not sure who snarked you down on this one.
ojblass
+1  A: 

Code that runs continually is called a daemon and there is an article here outlining how to do what you ask. That will point you to an example of how to write a simple service here.

ojblass
Yes thought a bout a service but don't think it will meet my needs as I need to process files and read configuration information from a stored file to so work at specific times as specified in the configuration file.
+2  A: 

You probably don't want to just spin in a loop needlessly consuming processor time.

Assuming you are on windows, you should have a loop that never ends with a call to WaitForSingleObject() or WaitForMultipleObjects() or MsgWaitForMultipleObjects() depending on your needs. Then have some synchronization object that wakes you up, such as a named event.

See the Win32 Synchronization documentation here. If you elaborate more on what your program needs to do, we can probably provide more specific advice.

jeffamaphone
Hi, Thanks for the update, the reason I selected to do this as a console application is that it then gives me the option of running it manually at any time. The application will read a config file and execute a file read/process at a given time from information stored in the config file.
Will also need to send an alert email if a file is missing from a certian directory. If the file exists it needs to be opened and read looking for errors, any errors then an alert email also needs to be sent.
A: 

Well I'm sure at some point it should stop, no?

Spawn a thread that does work, and have the main thread block on Console.ReadLine() if you want it to be runnable as a console app too.

If you really just want to pause the main thread forever, just block on a ManualResetEvent you never fire.

But, consider using a service if you can.

MichaelGG
+1  A: 

If your program is going to be continually running then you should sleep until the desired event occurs (e.g. XX seconds passes). If you just spin in a while {} loop you'll suck up CPU times.

If your program is going to always be running on a machine then you should consider making it a service so it automatically starts and stops with the machine.

Andrew Grant
+8  A: 

Better solution would be to write a console application that does its job and quits, and then use the Windows Task Scheduler to run it periodically.

jachymko
Absolutely agreed! Jon Galloway wrote a blog post a while back that convinced me that I was doing it wrong by making a service instead of a task: http://weblogs.asp.net/jgalloway/archive/2005/10/24/428303.aspx
Neil Williams
I think that services offer more flexibility in terms of user credentials.
ojblass
At least as far as I've seen you can set the credentials that the scheduled task will run under. What can you do with a service that you can't with a task?
Neil Williams
Bottom line is we don't know because OP didn't tell us what he's actually trying to do.
jeffamaphone
+1  A: 

If your building a desktop application you'll want to have it run in the system tray. This will

  1. Keep your users from accidentally closing the application
  2. Keep your application from cluttering the screen of your users

If your building a server application you will want to write a windows service. This will

  1. Keep an administrator from accidentally closing your application
  2. Eliminate the need for the server to have someone logged into the console for your application to be running in

As someone who is primarily an IT Pro I would say that 3rd party applications that we get that run as console apps instead of windows services we put a lot of effort into keeping from being purchased. It creates a lot of work for us, and opens up significant support issues and security holes.

Steve Evans