Click here to Skip to main content
16,019,427 members
Articles / Programming Languages / C#
Tip/Trick

How to Schedule a Task In .NET Framework (C#)

Rate me:
Please Sign up or sign in to vote.
3.00/5 (7 votes)
11 Nov 2017CPOL1 min read 38.9K   298   15   10
In this tip, you will see how to schedule a task in .NET Framework (C#).

Introduction

Sometimes, you want to schedule a task in .NET Framework...
For example, you want to run a code every night at 01:00 AM for creating reports, calling a service, sending a message, checking some flows, etc.

For such work, someone creates a batch file and runs it with windows scheduled task, but in some situation, it won't work correctly,

Someone creates a timer for checking the local time and runs their task at a specific time, but it does not make good performance.

In .NET Framework , you can use System.Management.ManagementEventWatcher library for watching on local time and raise an event at the desired time.

Using the Code

First of all, you must add reference to System.Managment.dll.

Image 1

We should create an instance of System.Management.WqlEventQuery and set the desired time on query.

In this example, I set query to run every day at 10:59:59 AM.

C#
System.Management.WqlEventQuery query = new System.Management.WqlEventQuery_
("__InstanceModificationEvent", new System.TimeSpan(0, 0, 1), _
"TargetInstance isa 'Win32_LocalTime' AND TargetInstance.Hour=10 AND _
TargetInstance.Minute=59 AND TargetInstance.Second=59");

Create an instance of System.Management.ManagementEventWatcher and pass your query in constructor:

C#
System.Management.ManagementEventWatcher watcher = new System.Management.ManagementEventWatcher(query);

Now, it's time to connect event handles method to event, it will connect to onEventArrived method.

C#
 watcher.EventArrived += new System.Management.EventArrivedEventHandler
(new System.Management.EventArrivedEventHandler(OnEventArrived));

And start the watcher:

C#
watcher.Start();

Finally, create onEventArrived method, this method will run every day at the desired time (in this sample, 10:59:59 AM).

C#
public static void OnEventArrived(object sender, System.Management.EventArrivedEventArgs e)
{
	// this method will run every day at 10:59:59 AM
}

This is all code at a glance in Console Application.

C#
static void Main(string[] args)
{
	System.Management.WqlEventQuery query = new System.Management.WqlEventQuery_
               ("__InstanceModificationEvent", new System.TimeSpan(0, 0, 1), _
               "TargetInstance isa 'Win32_LocalTime' AND TargetInstance.Hour=10 AND _
                TargetInstance.Minute=59 AND TargetInstance.Second=59");

	System.Management.ManagementEventWatcher watcher = _
                 new System.Management.ManagementEventWatcher(query);

	watcher.EventArrived += new System.Management.EventArrivedEventHandler_
                (new System.Management.EventArrivedEventHandler(OnEventArrived));

	watcher.Start();

	System.Console.ReadLine();
}

public static void OnEventArrived(object sender, System.Management.EventArrivedEventArgs e)
{
	System.Console.WriteLine("Event Arrived");
}

I hope you'll like this code and use it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionApplication Closed Pin
Member 151512739-Jun-21 8:31
Member 151512739-Jun-21 8:31 
QuestionTimeSpan Parameter Not Needed/Not Used Pin
KevinAG29-Dec-17 14:03
KevinAG29-Dec-17 14:03 
QuestionHow run it? Pin
ra_duque13-Nov-17 6:34
ra_duque13-Nov-17 6:34 
It must run in each system reboot or only one run is enough?
AnswerRe: How run it? Pin
KevinAG29-Dec-17 14:22
KevinAG29-Dec-17 14:22 
Answerwhen windows scheduled tasks doesn't work (a private experience) Pin
HamedKhatami11-Nov-17 2:51
HamedKhatami11-Nov-17 2:51 
GeneralRe: when windows scheduled tasks doesn't work (a private experience) Pin
OriginalGriff11-Nov-17 2:52
mveOriginalGriff11-Nov-17 2:52 
QuestionPlease explain Pin
Yks11-Nov-17 2:23
Yks11-Nov-17 2:23 
AnswerRe: Please explain Pin
HamedKhatami11-Nov-17 3:14
HamedKhatami11-Nov-17 3:14 
GeneralRe: Please explain Pin
Yks11-Nov-17 5:50
Yks11-Nov-17 5:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.