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
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.
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.
You can add a reference to System.Windows.Forms
and call System.Windows.Forms.Application.Run()
to begin a standard application message loop. Ctrl-C will terminate the app.
Another option is to use Console.ReadKey()
to pause the app. Like Console.WriteLine("Press [ANY] key to quit..."); Console.ReadKey();
That's what I use in my console apps when they're just sitting there waiting for events to occur. In either case the app will continue to run and catch triggered events (like from a timer, WCF, FileWatcher, etc).
If your building a desktop application you'll want to have it run in the system tray. This will
If your building a server application you will want to write a windows service. This will
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.
Why don't you build your app as a service in the first place?