History

History of .net

Thursday, 24 January 2013


Method to Add a Scheduler Dynamically through C# Code


Bfore writing this method we have to add a dll(Microsoft.Win32.TaskScheduler.dll) reference to our Application MUST
private bool CreateScheduler(string _newServicePath)
{
try
{
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Data Import Utility for " + txtShortCode.Text;
// Create a trigger that will fire the task at this time every other day
var tt = new TimeTrigger();
tt.Repetition.Interval = TimeSpan.FromMinutes(30);
td.Triggers.Add(tt);
// Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(new ExecAction(_newServicePath, null, null));
// Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition(txtShortCode.Text, td);
return true;
}
}
catch (Exception ex)
{
ex.ToString();
return false;
}
}

No comments:

Post a Comment