Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to WhenAll when some tasks can be null?

I would like to wait all task, but some of them can be null. It is a code like that:

Task<MyType1> myTask1 = getData01Async();
Task<MyTyp2> myTask2 = null;
Task<MyType3> myTask3 = null;

if(myVariable == true)
{
    myTask2 = getData02Async();
}
else
{
    myTask3 = getData03Async();
}


wait Task.WhenAll(myTask1, myTask2, myTask3);

The idea is, task1 always exists, but task2 and task3 depends of a variable. So I would like to run all the task in parallel and wait when all of them are finished. And if one task is null, the treat it as it is finished.

The problem with this code is that I get a null reference exception when it runs.

There are some way to do that? Or what another alternatives could I use?

like image 994
Álvaro García Avatar asked Jun 11 '18 14:06

Álvaro García


People also ask

Can a task be null?

Task represents the execution of the asynchronous method, so for an asynchronous method to return a null task is like telling the calling code "you didn't really just call this method" when of course it did. So, a Task / Task<T> returned from a method should never, ever be null .

How does task WhenAll work?

Task. WhenAll creates a task that will complete when all of the supplied tasks have been completed. It's pretty straightforward what this method does, it simply receives a list of Tasks and returns a Task when all of the received Tasks completes.

Does task WhenAll run in parallel?

WhenAll() method in . NET Core. This will upload the first file, then the next file. There is no parallelism here, as the “async Task” does not automatically make something run in in parallel.

What does WhenAll return?

The overloads of the WhenAll method that return a Task object are typically called when you are interested in the status of a set of tasks or in the exceptions thrown by a set of tasks. The call to WhenAll(IEnumerable<Task>) method does not block the calling thread.

How to wait for a set of tasks to complete?

We can use Task.WhenAll to wait for a set of tasks to complete. We can also wait for each task in a loop. But that’ll be inefficient since we dispatch the tasks one at the time. string content = await client. GetStringAsync ( uri ); As you can see in the method above, we call GetStringAsync method one at the time.

What is a null task in Revit?

A null task is a blank row used to delimit some phases in your project or for any visual arrangements. How do I insert a single blank row? To insert a single blank row, select row/cell above which you want to insert the new row.

What is the use of whenall method in Java?

The overloads of the WhenAll method that return a Task object are typically called when you are interested in the status of a set of tasks or in the exceptions thrown by a set of tasks. The call to WhenAll(Task[]) method does not block the calling thread.

What happens if the tasks argument contains no tasks?

If the tasks argument contains no tasks, the returned task will immediately transition to a RanToCompletion state before it's returned to the caller. The returned TResult [] will be an array of 0 elements.


4 Answers

Just filter the null tasks out:

await Task.WhenAll(new Task[] { task1, task2, task3 }.Where(i => i != null));
like image 156
armenm Avatar answered Oct 26 '22 22:10

armenm


Well, depending on your scenario you could assign completed tasks or put them in an array/list and then pass this list to WhenAll:

Task<MyType1> myTask1 = getData01Async();
Task<MyType2> myTask2 = Task.FromResult((MyType2) null);
Task<MyType3> myTask3 = Task.FromResult((MyType3) null);

...

await Task.WhenAll(myTask1, myTask2, myTask3);
like image 40
Fabjan Avatar answered Oct 26 '22 22:10

Fabjan


Use a collection to track the tasks that aren't null. Then pass that list to Task.WhenAll method like below:

var tasks = new List<Task>();

Task<MyType1> myTask1 = getData01Async();
tasks.Add(myTask1);

Task<MyTyp2> myTask2 = null;
Task<MyType3> myTask3 = null;

if(myVariable == true)
{
    myTask2 = getData02Async();
    tasks.Add(myTask2);
}
else
{
    myTask3 = getData03Async();
    tasks.Add(myTask3);
}

await Task.WhenAll(tasks);
like image 37
CodeNotFound Avatar answered Oct 26 '22 23:10

CodeNotFound


To build on what @UweKeim suggested, why not simply filter out the null tasks when you pass them to the WhenAll.

public async Task FilterNullTasks()
{
    Task<string> myTask1 = Task.Delay(1000).ContinueWith(tsk => string.Empty);
    Task<int> myTask2 = null;
    Task<bool> myTask3 = null;

    await Task.WhenAll(new Task[]
    {
        myTask1, myTask2, myTask3
    }.Where(tsk => tsk != null));
}
like image 41
JSteward Avatar answered Oct 26 '22 23:10

JSteward