Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Differences between Result and ContinueWith

What are the differences between this two methods that seems to do the same thing? Can it be done even with async/await?

public Task<int> TaskMaxAsync1 ( Task<int>[] my_ints )
{
    return Task.WhenAll( my_ints )
    .ContinueWith ( x => x.Result.Where ( i => i%2 != 0 ).Max( ) ) ;
}

public Task<int> TaskMaxAsync2 ( Task<int>[] my_ints )
{
    var numbers = Task.WhenAll( my_ints ).Result ;
    return Task.FromResult( numbers.Where( i => i%2 != 0 ).Max( ) ) ;
}
like image 874
PCH Avatar asked Aug 24 '15 08:08

PCH


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C full form?

Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


3 Answers

Differences between Result and ContinueWith

Result will synchronously block until the task completes, and will wrap exceptions in an AggregateException.

ContinueWith will register a callback with the task, and invoke that callback when the task completes.

For asynchronous code, both Result and ContinueWith should be replaced by await.

Can it be done even with async/await?

Sure, like this:

public async Task<int> MaxAsync(Task<int>[] my_ints)
{
  int[] ints = await Task.WhenAll(my_ints);
  return ints.Where(i => i % 2 != 0).Max();
}
like image 170
Stephen Cleary Avatar answered Oct 24 '22 23:10

Stephen Cleary


What are the differences between this two methods that seems to do the same thing?

The difference is that the former returns a hot task to the caller, while the latter synchronously blocks, then re-wraps the result in a Task using Task.FromResult. The latter is also a common case for deadlocks, if you're running inside an environment that has a custom SynchronizationContext.

Can it be done even with async/await?

Yes, it can:

public async Task<int> MaxAsync(Task<int>[] myInts)
{
    int[] results = await Task.WhenAll(myInts);
    return results.Max(i => i % 2 != 0 ? i : (int?)null) ?? 0;
}
like image 27
Yuval Itzchakov Avatar answered Oct 24 '22 23:10

Yuval Itzchakov


task0.Result will synchronously block and wait for task0 to complete, while task1.ContinueWith will not wait for task1 to complete but returns a new Task(which will run when task1 completes) immediately.

So, the two methods in your example do not behave the same. In your second method, if my_ints were not all ran to complete when they were passed to the WhenAll method, the .Result will synchronously block the calling thread for whatever long time that all tasks within my_ints would take to finish, and if some of them throws exception, TaskMaxAsync2 throws.

However the first method will return immediately even my_ints would never finish or throw exception.

like image 22
Paul Chen Avatar answered Oct 24 '22 23:10

Paul Chen