Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task.Factory.StartNew with parameters and return values

Tags:

c#

Trying to call a method that requires parameters in order to get a result and pass the result to proceed. But I'm new to the Task area and can't seem to figure out the correct syntax. Any help would be appreciated.

Task.Factory.StartNew(() => 
    CheckConflict(startDate, endDate, actID, repeatRule,whichTime))
    .ContinueWith(
        GetConflictDelegate(result),
        TaskScheduler.FromCurrentSynchronizationContext);
like image 297
Rick Avatar asked Mar 10 '14 03:03

Rick


1 Answers

Assuming you want to continue with the result of CheckConflict(), ContinueWith takes a Task<T> as an argument. Task<T> has a property Result, which will be the result from the method invocation.

See my code snippet below, for an example.

new TaskFactory()
.StartNew(() =>
    {
        return 1;
    })
.ContinueWith(x =>
    {
        //Prints out System.Threading.Tasks.Task`1[System.Int32]
        Console.WriteLine(x);
        //Prints out 1
        Console.WriteLine(x.Result);
    });
like image 96
Xenolightning Avatar answered Nov 05 '22 12:11

Xenolightning