Run a specific task in Task Scheduler using ASP.NET C#

2.8k Views Asked by At

I have a button in ASP.Net Web Application. I want this button to invoke a task in task scheduler. I didn't find any exact code by which I could invoke a task from a task scheduler. Please Help....!!!

2

There are 2 best solutions below

0
On

Have a look at the documentation for this library. It is a wrapper for Windows Task Scheduler.

http://taskscheduler.codeplex.com

which is a wrapper for Task Scheduler Windows API https://msdn.microsoft.com/en-us/library/windows/desktop/aa383614(v=vs.85).aspx

using this wrapper you can interact with the task scheduler easily with code like this

using (TaskService ts = new TaskService())
{
  Task t = ts.GetTask(taskName);
  if (t != null)
  {
      // get status here or get runtime
      var isEnabled = t.Enabled;
      var runs = t.GetRunTimes(startDate,endDate);
  }
}

now you should remember that you can not poll the status of the task while you are holding up the ASP.NET request processing cycle. Probably the best you can do is checking the status in Ajax calls until the task is finished.

Meanwhile re-evaluate your design and the suitability of the task scheduler for this purpose. Securing your website while allowing access to task scheduler might be a challenge. One other option would be a Windows Service that you develop and runs requested tasks for the Web application. This has always worked perfectly for me. The client can be ASP.NET AJAX or JavaScript getting the status of the task through Web API

1
On