Arch1tect
Arch1tect

Reputation: 4281

Should I Use An Always-Running Console Program on Server to Do Task

What I want to achieve is quite simple, and it can be accomplished by a always-running console program like below:

class Program
{
    static void Main(string[] args)
    {

        while (true) {

            DateTime now = DateTime.Now;
            if(now==timeToRunTask)
                 runMyTask();//e.g. send an email to Joe

        }
    }
}

I wonder if this approach is very inefficient in terms of what I'm trying to achieve. And I saw some information about using Task Scheduler so I wonder if I should use that. I'm using Windows Server. But I'd like to know why I shouldn't use the above approach first. Thanks!

Upvotes: 2

Views: 427

Answers (2)

bstenzel
bstenzel

Reputation: 1241

On a server you have little to no control over the app. E.g. if the server reboots (due to installed updates for example), you have to restart the app manually.

If you only have this one app, I'd recommend making it a windows service. It's incredibly simple to do in C#. If you need multiple tasks in the long run, having multiple services (maybe even on multiple servers) for it can become quite the hassle, though.

Upvotes: 1

Jon Tirjan
Jon Tirjan

Reputation: 3694

Even if you stick with this approach, you still need a way to launch the console app. You can't just log in and run it, because it will terminate when you log off.

The simplest approach is to use the tools Windows provides, in this case Task Scheduler. You can configure a task to launch your application on an interval. Then in the application, just run the task once and exit.

If you truly require that the application stays running indefinitely, I recommend moving from a console app to a Windows Service.

Upvotes: 2

Related Questions