Controlling is behaviour of Task Scheduler is very easy. Follow these steps:
Step 1:
Add Microsoft.Win32.TaskScheduler dll to your project. Download it if not available.
Step 2:
I guess if you workingin winform then take a datagridview.
Step 3:
Here the code part.
using Microsoft.Win32.TaskScheduler;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = AllTaskNames();
}
public List<SchedulerModel> GetAllTasks()
{
List<SchedulerModel> TaskList = new List<SchedulerModel>();
using (TaskService ts = new TaskService())
{
var tst = ts.GetFolder(@"\Microsoft\MyTasks");
if (tst != null)
{
var tasks = tst.AllTasks;
foreach (var t in tasks)
{
SchedulerModel sM = new SchedulerModel();
sM.Name = t.Name;
sM.Status = t.State.ToString();
sM.LastRunTime = t.LastRunTime;
sM.NextRunTime = t.NextRunTime.ToShortDateString() == "1/1/0001" ? (DateTime?)null : t.NextRunTime;
TaskList.Add(sM);
}
}
}
return TaskList;
}
public void DisableAnyTask()
{
TaskService ts = new TaskService();
var t = ts.GetFolder(@"\Microsoft\MyTasks");
if (t != null)
{
var tasks = t.AllTasks;
foreach (var ee in tasks)
{
if (ee.Name.ToLower() == "task one")
{
if (ee.State.ToString() == "Running")
{
ee.Stop();
ee.Enabled = false;
}
}
}
}
}
}
public class SchedulerModel
{
public string Name { get; set; }
public string Status { get; set; }
public DateTime LastRunTime { get; set; }
public DateTime? NextRunTime { get; set; }
}
Step 4:
On Page_Load Call below written function As
dataGridView1.DataSource = GetAllTasks();
So, You have all tasks available of that particular location.
Note:
> You must have rights to access these task so, make sure you using Visual Studio as Administrator.
> Ctrl + R >> Taskschd.msc: here you have Task Scheduler window open you have to make a folder (MyTask)
Path : Task Scheduler Library > Microsoft > MyTask
MyTask: Name of you folder where you have to create your tasks.
Thanks!
Cheers!!!
Step 1:
Add Microsoft.Win32.TaskScheduler dll to your project. Download it if not available.
Step 2:
I guess if you workingin winform then take a datagridview.
Step 3:
Here the code part.
using Microsoft.Win32.TaskScheduler;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = AllTaskNames();
}
public List<SchedulerModel> GetAllTasks()
{
List<SchedulerModel> TaskList = new List<SchedulerModel>();
using (TaskService ts = new TaskService())
{
var tst = ts.GetFolder(@"\Microsoft\MyTasks");
if (tst != null)
{
var tasks = tst.AllTasks;
foreach (var t in tasks)
{
SchedulerModel sM = new SchedulerModel();
sM.Name = t.Name;
sM.Status = t.State.ToString();
sM.LastRunTime = t.LastRunTime;
sM.NextRunTime = t.NextRunTime.ToShortDateString() == "1/1/0001" ? (DateTime?)null : t.NextRunTime;
TaskList.Add(sM);
}
}
}
return TaskList;
}
public void DisableAnyTask()
{
TaskService ts = new TaskService();
var t = ts.GetFolder(@"\Microsoft\MyTasks");
if (t != null)
{
var tasks = t.AllTasks;
foreach (var ee in tasks)
{
if (ee.Name.ToLower() == "task one")
{
if (ee.State.ToString() == "Running")
{
ee.Stop();
ee.Enabled = false;
}
}
}
}
}
}
public class SchedulerModel
{
public string Name { get; set; }
public string Status { get; set; }
public DateTime LastRunTime { get; set; }
public DateTime? NextRunTime { get; set; }
}
Step 4:
On Page_Load Call below written function As
dataGridView1.DataSource = GetAllTasks();
So, You have all tasks available of that particular location.
Note:
> You must have rights to access these task so, make sure you using Visual Studio as Administrator.
> Ctrl + R >> Taskschd.msc: here you have Task Scheduler window open you have to make a folder (MyTask)
Path : Task Scheduler Library > Microsoft > MyTask
MyTask: Name of you folder where you have to create your tasks.
Thanks!
Cheers!!!
No comments:
Post a Comment