1
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

Task tasks = PeriodicTaskFactory.Start(() => LongRunningOperation(), intervalInMilliseconds: 1000, synchronous: false, cancelToken: cancellationTokenSource.Token);

int taskId = Task.WaitAny(tasks);

and my LongRunningOperation

private String LongRunningOperation()
{
...
return proj.Name;
}

but the problem is how to get value from LongRunningOperation method back in Task. Method tasks.Results not exists. And I want to get back the value from each Task.

I get PeriodicTaskFactory class from here

Is there a Task based replacement for System.Threading.Timer?

Thank you

Community
  • 1
  • 1
senzacionale
  • 20,448
  • 67
  • 204
  • 316
  • If `Start` returned a single `Task`, then you wouldn't be able to use `Task.WaitAny` on it. I think you meant that `Start` returns `Task[]`. – dcastro Nov 28 '13 at 11:37
  • @dcastro "Start" return back "return Task.Factory.StartNew(mainAction, cancelToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);" – senzacionale Nov 28 '13 at 11:56
  • Then this line is wrong: `int taskId = Task.WaitAny(tasks);`. There is no `WaitAny` overload that accepts a single task. – dcastro Nov 28 '13 at 12:02
  • Thank you. And what is then correct. The code you paste it in reply? – senzacionale Nov 28 '13 at 12:07
  • The code in my reply helps you get a result back. Don't you get a compile error with the code you posted? What exactly are you trying to achieve? – dcastro Nov 28 '13 at 12:09
  • No code is working without any error. I want to run Task every x seconds and executing some data. This needs to be then returned back for further analyze. But important is that everything is working async. More tasks at the same time. – senzacionale Nov 28 '13 at 12:12
  • The line with `WaitAny` doesn't make any sense, but that's not your question. My answer shows you how to grab the result of the task returned by the `StartPeriodicTask`. – dcastro Nov 28 '13 at 12:17
  • WaitAny is used that wait until one task is finished. Isn't that true? – senzacionale Nov 28 '13 at 12:33
  • No, `WaitAny` waits until one **of many** tasks is finished. Take a look at the msdn documentation, all overloads take an array of Tasks: http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.waitany(v=vs.110).aspx – dcastro Nov 28 '13 at 12:35

1 Answers1

4

Task has no return value; Task<T> does.

You have to modify the PeriodicTaskFactory code to return Task<T> instead, and then create Task<string> objects. The method should also accept a Func<T> (has no arguments, returns T) instead of an Action, which has no return value.

public static Task<T> Start<T>(Func<T> func,
      int intervalInMilliseconds = Timeout.Infinite,
      int delayInMilliseconds = 0,
      int duration = Timeout.Infinite,
      int maxIterations = -1,
      bool synchronous = false,
      CancellationToken cancelToken = new CancellationToken(),
      TaskCreationOptions periodicTaskCreationOptions = TaskCreationOptions.None)
    {


Task<string> task = PeriodicTaskFactory.Start(LongRunningOperation, intervalInMilliseconds: 1000, synchronous: false, cancelToken: cancellationTokenSource.Token);

string result = task.Result;
dcastro
  • 66,540
  • 21
  • 145
  • 155