fubo
fubo

Reputation: 45947

Task.Factory.StartNew Passing return values

i gote the following method and i want to pass the return values of the dosleep method:

static void Main(string[] args)
{
    var t1 = Task.Factory.StartNew(() => dosleep(2000));
    var t2 = Task.Factory.StartNew(() => dosleep(1000));
    Task.WaitAll(t1,t2);
    Console.WriteLine("All Done in {0} milliseconds!"); //t1+t2 here!!
}

public static int dosleep(int Milliseconds)
{
    System.Threading.Thread.Sleep(Milliseconds);
    Console.WriteLine("Task finished");
    return Milliseconds;
}

Upvotes: 1

Views: 1986

Answers (1)

Stilgar
Stilgar

Reputation: 23551

Use the Result property of the Task object

Console.WriteLine("All Done in {0} milliseconds!", t1.Result + t2.Result); 

Upvotes: 5

Related Questions