locked
How to start TPL after some duration RRS feed

  • Question

  • I will be some task to TPL(Task parallel library) via

     Task.Factory.StartNew(() => Method, System.Threading.CancellationToken.None,
                                 TaskCreationOptions.LongRunning, TaskScheduler.Default);

    What i need was the task which was assigned need to start after certain interval say for example 5 -10 minutes. For that is TPL have any inbuilt overload/properties to achieve that or we should use somethin like Thread.sleep() or wait .

    Suggest some optimal solutions

    Friday, September 25, 2015 8:58 AM

Answers

  • 1. use Task.Delay (or)

    2. use new Task method just create your task and call the task.start method whever you want run the task

    ex : Task mytaskDelay;

    mytaskDelay = new Task(...); //Create a task but it wont run

    mytaskDelay.Start(); // run the task whenever you want.


    Vinoth Rajendran

    • Proposed as answer by VinothRajendran Friday, September 25, 2015 9:25 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:45 AM
    Friday, September 25, 2015 9:25 AM
  • You should be aware that jsut because you said to start the task does not mean it actually starts right there and then. Depending on system load and considering you marked this one as LongRunning for the ThreadPool it's execution might begin long after you requested it's start.
    And nobody guarantees it will be finished in N minutes either.

    I would advise against just trying to start it every N minutes. Rather limit it to not run more often then every N minutes. i.e., start this as a persistent Thread:

    DateTime nextDueTime = DateTime.Now;
    
    while(!quitTask){
      if(DateTime.Now >= nextDueTime){
        nextDueTime = DateTime.Now.AddMinutes(interval);
        //execute your code here
      }
      else{
        //Sleep 1 second so this polling won't tax out the CPU.
        Thread.Sleep(1000);
      }
    }

    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:45 AM
    Friday, September 25, 2015 10:29 AM

All replies

  • 1. use Task.Delay (or)

    2. use new Task method just create your task and call the task.start method whever you want run the task

    ex : Task mytaskDelay;

    mytaskDelay = new Task(...); //Create a task but it wont run

    mytaskDelay.Start(); // run the task whenever you want.


    Vinoth Rajendran

    • Proposed as answer by VinothRajendran Friday, September 25, 2015 9:25 AM
    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:45 AM
    Friday, September 25, 2015 9:25 AM
  • You should be aware that jsut because you said to start the task does not mean it actually starts right there and then. Depending on system load and considering you marked this one as LongRunning for the ThreadPool it's execution might begin long after you requested it's start.
    And nobody guarantees it will be finished in N minutes either.

    I would advise against just trying to start it every N minutes. Rather limit it to not run more often then every N minutes. i.e., start this as a persistent Thread:

    DateTime nextDueTime = DateTime.Now;
    
    while(!quitTask){
      if(DateTime.Now >= nextDueTime){
        nextDueTime = DateTime.Now.AddMinutes(interval);
        //execute your code here
      }
      else{
        //Sleep 1 second so this polling won't tax out the CPU.
        Thread.Sleep(1000);
      }
    }

    • Marked as answer by Kristin Xie Thursday, October 8, 2015 3:45 AM
    Friday, September 25, 2015 10:29 AM
  • @VinothRajendran

    I have used the code as shown below 

     Task.Delay(Math.Abs(date.Minute - DateTime.UtcNow.Minute) * 1000 * 60);//It should wait for that milliseonds but thats not happening.

     Task.Factory.StartNew(() =>Action,Token,
                                 TaskCreationOptions.LongRunning, TaskScheduler.Default);

    But the thread does not waits for that milliseconds instead it progress for the next. Say for example if the task should start at 11:00 when the code hits the time was 10:55 so the thread should wait for 5 minutes and then proceed but thats not happening . Process occurs at 10:06 itself.

    Wednesday, September 30, 2015 11:07 AM
  • Hi,

    Please try Task.Delay().Wait , next line wont process until delay function return

    ex: Task.Delay(Math.Abs(date.Minute - DateTime.UtcNow.Minute) * 1000 * 60).Wait();


    Vinoth Rajendran

    Wednesday, September 30, 2015 12:56 PM