Console Application Using Windows Scheduler

In our software development life, sometimes we get a requirement such as to execute a task on a daily, weekly and monthly basis.

For instance, we need to send an e-mail or do a file transfer on a specified frequency based on a condition.

How to do it

As in the preceding scenario we can have multiple solutions for that:

  1. Create a Windows Service, implement your logic and a set timer.
  2. Create a console application and implement your logic, then create a scheduler and refer action as console application .exe file.
  3. Any third-party scheduler like Quartz scheduler.

Here we will explain use of a console application with the Windows Scheduler.

To accomplish the preceding task please find the following procedure.

Step 1

First we need to create a console application in Visual Studio. Please see the following screenshot, I have created a console app with starter logic:

console application

Step 2

After implementing our logic we need to build the application in release mode. Once it builds successfully, it creates one .exe file in your directory. Please see the following screenshot:

file in your directory

Step 3

Now my application is ready for setting up in the Windows Scheduler.

Before you start the scheduler you should have Admin Rights in system.

To open Windows Scheduler go to the Start menu and in the search box type "Scheduler", then it will display the Task Scheduler. Click on that.

Please see the following screenshot:

Scheduler

Step 4

After clicking on it, a window will open where you can create a task. Click on the "Create Task" link on the right side and it opens the "Create Task" window. In this window we have a couple of tabs, like General, Triggers, Actions and so on.

In the General tab we need to provide the scheduler name and description details. Please see the following screenshot:

General tab

In the Triggers tab we need to set when the scheduler will start and what the time interval details are.

In the following screenshot, I have set it depending on my requirements. In other words I have set here that my .exe should execute at one minute intervals for an indefinite amount of time. In the screen I have checked Daily, Repeat task every day with 1 minute.

So you need to set it depending on your requirements, either daily, weekly or monthly.

set as per your requirement

In the Actions tab we need to set only what the scheduler will execute.

In the following screenshot I have selected the Action as "Select Program" because we need to execute an .exe file.

Next browse to where your exe file is present in your directory. I have already discussed when our console app successfully executes. It generates an exe file (please refer to the Step 2 screenshot). Click OK.

directory

Step 5

Once everything is set up, our scheduler will begin to run.

If everything is properly coded then you will get correct output as in the screenshot. My scheduler is generating text files at 1 minute intervals.

generating text files

I hope this will help you.
Happy coding !!!

Up Next
    Ebook Download
    View all
    Learn
    View all