Setup A Task Scheduler To Run Application Periodically In C#

Developers often require to create and run background tasks that run periodically. For example, email reminders, notifications, send weekly emails, printing processes, calculations, auto updates, data syncing and so on. We can easily create background tasks and schedule them using C#. We can do that using a task scheduler. 
 
In this tutorial, let's learn how to create a C# console application that executes some tasks, and schedule and execute it using a task scheduler. Our app is a console app that writes to a text file. We will use the Task Scheduler to schedule the task of execution of the console app and then we will test it.
 
  

Step 1. Create a Console App

  1. Open Visual Studio 2019.
  2. On the start window, choose Create a new project.
  3. On the Create a new project window, enter or type console in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list.
Setup Task Scheduler To Run Application
 
After you apply the language and platform filters, choose the Console App (.NET Framework) template, and then choose Next.
 
Setup Task Scheduler To Run Application
 
Give the project a name and browse location and click Create.
 
Let’s add some code in class. In the given lines of code I am creating a text file with some text and current date and time, so every time the task runs, we should have the date and time.
 
Setup Task Scheduler To Run Application
 

What is Windows Task Scheduler?

 
The Task Scheduler is a Windows component that can automatically run tasks at a specific time or in response to a specific event. For example, tasks can be run when the computer starts or when a user logs in. You can start tasks when a specific event occurs, and using task scheduler you can run commands and execute scripts at a specific day and time.
 

Step 2. Create a Task using Task Scheduler

 
Search task scheduler in Windows search.
 
Setup Task Scheduler To Run Application
 
Right click on Task Scheduler and click New Folder.
 
Setup Task Scheduler To Run Application
 
Right click on folder and click Create Task.
 
Setup Task Scheduler To Run Application
 
In General tab, provide task name and description.
 
Setup Task Scheduler To Run Application
 
In Trigger tab, create new and provide task schedule date, time, and day.
 
Setup Task Scheduler To Run Application
 
In Actions tab, click New and give Action step and Browse console application executable file and click OK.
 
"C:\Users\rkumar285\source\repos\TaskSchedularSample\TaskSchedularSample\bin\Debug\TaskSchedularSample.exe"
 
Setup Task Scheduler To Run Application
 
Right click on created task and Run.
 
Setup Task Scheduler To Run Application
 
Once you click on Run , as you can see, the current status is changed to Running.
 
Setup Task Scheduler To Run Application
 

Step 3. Test

 
Now go to the directory where we are creating a text file to see if the task is running properly or not.
 
Setup Task Scheduler To Run Application
 
As you can see test.txt file is created in temp folder. When open text file, you will see given text with current date and time. Since we have scheduled our job to run every 5 minutes, the current date and time will change every 5 minutes when the job runs.
 
Let’s wait for 5 minutes to create a new text in test file. After five minutes, as you can see, the file has a new date and time.
 
Setup Task Scheduler To Run Application
 

Conclusion

 
In this article, we have learned how to create a new task and set up a console application using Windows 10 Task Scheduler.