Task.Factory.StartNew(someMethod(withParam)).continueWith(sameMethod(differentParam)).Wait() - c#

What is the correct syntax for parallelizing the following code?
static void Main(string[] args)
{
Task.Factory.StartNew(
() =>
doOne(SelectedTask.option1)
.ContinueWith(
task =>
doOne(SelectedTask.option1)).Wait()
);
}
Same method with enum "selectedTask" to decide which code to execute :
static enum SelectedTask
{
option1,
option2
}
static void doOne(SelectedTask Lunch)
{
switch (lunch)
{
case SelectedTask.option1:
Console.WriteLine("option1");
break;
case SelectedTask.option2:
Console.WriteLine("option2");
break;
default:
break;
}
}

Do you want your doOne calls to occur concurrently? Then you can just start them straight from the task factory:
// Start two concurrent tasks
var task1 = Task.Factory.StartNew(() => doOne(SelectedTask.option1));
var task2 = Task.Factory.StartNew(() => doOne(SelectedTask.option2));
// Block the current thread until all tasks complete
Task.WaitAll(task1, task2);
Do you want your doOne calls to occur sequentially? Then you can chain them using ContinueWith:
// Start a chain of tasks
var task1 = Task.Factory.StartNew(() => doOne(SelectedTask.option1));
var task2 = task1.ContinueWith(t => doOne(SelectedTask.option2));
// Block the current thread until the last task completes
task2.Wait();
The code in the title of your post (with a couple of fixes) is essentially performing the exact same function as my sequential task chain above:
Task.Factory.StartNew(() => doOne(SelectedTask.option1))
.ContinueWith(t => doOne(SelectedTask.option2))
.Wait();
Answer to your question below.
If I understand correctly, you want to be able to run a task for a variable list of SelectedTasks in parallel:
List<SelectedTask> selectedTaskOptions = new List<SelectedTask>()
{
SelectedTask.option1,
SelectedTask.option2,
SelectedTask.option3,
SelectedTask.option4,
SelectedTask.option5
};
RunAllSelectedTaskOptions(selectedTaskOptions);
RunAllSelectedTaskOptions to accept and run a list of SelectedTasks:
public void RunAllSelectedTaskOptions(List<SelectedTask> selectedTaskOptions)
{
List<Task> createdTasks = new List<Task>();
foreach(var taskOption in selectedTaskOptions)
{
createdTasks.Add(Task.Factory.CreateNew(() => doOne(taskOption)));
}
Task.WaitAll(createdTasks);
}
Another way of implementing RunAllSelectedTaskOptions would be to use Parallel.ForEach, which will execute in parallel and will block until the slowest/last iteration has completed:
public void RunAllSelectedTaskOptions(List<SelectedTask> selectedTaskOptions)
{
Parallel.ForEach(selectedTaskOptions, taskOption => doOne(taskOption));
}

I assume you are talking about parallelizing the two doOne calls?
If so, then you will need to do something like this:
var task1 = Task.Factory.StartNew(() => doOne(SelectedTask.option1));
var task2 = Task.Factory.StartNew(() => doOne(SelectedTask.option2));
var taskList = new List<Task>{task1, task2};
Task.WaitAll(taskList);
*The above code is fairly accurate but the syntax has not been validated.

Related

Running Tasks from List

I am trying to run a task List using the following method:
List<Task> tasks = new List<Task>();
tasks.Add(new Task(() => this.firstMethod()));
tasks.Add(new Task(() => this.secondMethod()));
However, if I use one of the two examples below I get the following issues:
foreach (Task task in tasks)
{
await Task.Run(() => task);
}
In this first case, the tasks don't run at all.
foreach (Task task in tasks)
{
task.Start();
task.Wait();
}
In this second case, it runs only once, then I get the following error:
Start may not be called on a task that has completed
What am I missing?
You cannot re-use a Task. So let's start with creating an array of delegates
List<Action> tasks = new List<Action>();
tasks.Add(this.firstMethod);
tasks.Add(this.secondMethod);
and then run them sequentially, on additional threads (leaving the main thread free to update the UI):
foreach (Action task in tasks)
{
await Task.Run(task);
}
but this loop could be done in many ways, depending on the context of the calling code and the nature of the payloads.
This may be not the best solution.
Here's a unit test that shows how you can do this:
public class TasksTests
{
private readonly ITestOutputHelper _output;
public TasksTests(ITestOutputHelper output)
{
_output = output ?? throw new ArgumentNullException(nameof(output));
}
[Fact]
public async Task CanCreateAndRunTasks()
{
var tasks = new List<Task>
{
new Task(() => _output.WriteLine("Task #1")),
new Task(() => _output.WriteLine("Task #2"))
};
tasks.ForEach(t => t.Start());
await Task.WhenAll(tasks);
}
}
You first create the tasks. Then you need to start them. Lastly, you need to await them all, e.g., using Task.WhenAll.
the first case sample
foreach (Task task in tasks)
{
await Task.Run(() => task);
}
should be changed to
foreach (Task task in tasks)
{
task.Start(); // there is no sense to await since tasks should be run in parallel I suppose
}
The second case is not clear however.
Do you run the second case after the first one ? It should be fine if you have tasks just initialized before the Start call, like
List<Task> tasks = new List<Task>()
{
new Task(() => this.firstMethod()),
new Task(() => this.secondMethod()),
};
foreach (Task task in tasks)
{
task.Start();
task.Wait(); // this causes tasks to be run in sequence (one by one) like with await in the first sample
}
the most simplest case is to use Task.Run
var tasks = new[Task]
{
Task.Run(firstMethod),
Task.Run(secondMethod),
}; // tasks are started immediately there is no need to start them later
Task.WaitAll(tasks); // wait until all tasks are finished

How to determine the task (which using await inside) completed or not? [duplicate]

I'm trying to use Task.WaitAll on a list of tasks. The thing is the tasks are an async lambda which breaks Tasks.WaitAll as it never waits.
Here is an example code block:
List<Task> tasks = new List<Task>();
tasks.Add(Task.Factory.StartNew(async () =>
{
using (dbContext = new DatabaseContext())
{
var records = await dbContext.Where(r => r.Id = 100).ToListAsync();
//do long cpu process here...
}
}
Task.WaitAll(tasks);
//do more stuff here
This doesn't wait because of the async lambda. So how am I supposed to await I/O operations in my lambda?
Task.Factory.StartNew doesn't recognise async delegates as there is no overload that accepts a function returning a Task.
This plus other reasons (see StartNew is dangerous) is why you should be using Task.Run here:
tasks.Add(Task.Run(async () => ...
This doesn't wait because of the async lambda. So how am I supposed to
await I/O operations in my lambda?
The reason Task.WaitAll doesn't wait for the completion of the IO work presented by your async lambda is because Task.Factory.StartNew actually returns a Task<Task>. Since your list is a List<Task> (and Task<T> derives from Task), you wait on the outer task started by StartNew, while ignoring the inner one created by the async lambda. This is why they say Task.Factory.StartNew is dangerous with respect to async.
How could you fix this? You could explicitly call Task<Task>.Unwrap() in order to get the inner task:
List<Task> tasks = new List<Task>();
tasks.Add(Task.Factory.StartNew(async () =>
{
using (dbContext = new DatabaseContext())
{
var records = await dbContext.Where(r => r.Id = 100).ToListAsync();
//do long cpu process here...
}
}).Unwrap());
Or like others said, you could call Task.Run instead:
tasks.Add(Task.Run(async () => /* lambda */);
Also, since you want to be doing things right, you'll want to use Task.WhenAll, why is asynchronously waitable, instead of Task.WaitAll which synchronously blocks:
await Task.WhenAll(tasks);
You can do like this.
void Something()
{
List<Task> tasks = new List<Task>();
tasks.Add(ReadAsync());
Task.WaitAll(tasks.ToArray());
}
async Task ReadAsync() {
using (dbContext = new DatabaseContext())
{
var records = await dbContext.Where(r => r.Id = 100).ToListAsync();
//do long cpu process here...
}
}
you have to use the Task.ContinueWith method. Like this
List<Task> tasks = new List<Task>();
tasks.Add(Task.Factory.StartNew(() =>
{
using (dbContext = new DatabaseContext())
{
return dbContext.Where(r => r.Id = 100).ToListAsync().ContinueWith(t =>
{
var records = t.Result;
// do long cpu process here...
});
}
}
}

How to delay 'hot' tasks so they can processed in a set order

Say I have a set of tasks:
var task1 = DoThisAsync(...);
var task2 = DoThatAsync(...);
var task3 = DoOtherAsync(...);
var taskN...
I am looking for a way to process a set of tasks in order (determined by place in containing collection say), but to have the tasks only run/start when its their turn and not before - and have all of that wrapped up in its own task.
Problem constraints / details are:
These tasks need to be performed in a certain order i.e.task1, task2,...
The previous task must complete asynchronously before the next can start
The number of tasks is variable
A different set of tasks may be nominated each time code is run
The order and number of tasks is known in advance.
The main problem is that as soon as I call the relevant method (like DoThis()...) to return each task, that task is already 'hot' or running, violating (2) above.
I have tried working with.ContinueWith(..) , but if I call each of tasks like above to set the continuations or add them to a list or collection they've already started.
Not sure if Lazy < T > might help but can't see how at present?
Hope this makes sense as I'm fairly new to async / await / tasks.
Many thanks in advance.
Calling a method runs code. If you want an object that will call this method later, then use a delegate.
In this case, you could use Func<Task>, which is an asynchronous delegate. A list of these should suffice:
// Build the list of operations in order.
var operations = new List<Func<Task>>();
operations.Add(() => DoThisAsync(...));
operations.Add(() => DoThatAsync(...));
operations.Add(() => DoOtherAsync(...));
// Execute them all one at a time.
foreach (var operation in operations)
await operation();
you can simply create tasks with its constructor and then, call execution with .Start() methods.
Here an example:
var taskList = InitQueue();
foreach (var t in taskList.OrderBy(i => i.Order))
{
//Here I can skedule an existing task
t.TaskToRun.Start();
t.TaskToRun.Wait();
Console.WriteLine($"Task {t.Order} has finished its job");
}
public class TaskQueue : List<TaskItem>
{
}
public class TaskItem
{
public int Order { get; set; }
public Task TaskToRun { get; set; }
}
private static TaskQueue InitQueue()
{
var queue = new TaskQueue();
queue.Add(new TaskItem
{
Order = 1,
TaskToRun = new Task(() =>
{
Task.Delay(500);
Console.WriteLine("Hello from task 1");
})
});
queue.Add(new TaskItem
{
Order = 4,
TaskToRun = new Task(() => Console.WriteLine("Hello from task 4"))
});
queue.Add(new TaskItem
{
Order = 3,
TaskToRun = new Task(() =>
{
Task.Delay(5000);
Console.WriteLine("Hello from task 3");
})
});
queue.Add(new TaskItem
{
Order = 2,
TaskToRun = new Task(() => Console.WriteLine("Hello from task 2"))
});
return queue;
}

Is it OK to do some async/await inside some .NET Parallel.ForEach() code?

Given the following code, is it OK to do async/await inside a Parallel.ForEach ?
eg.
Parallel.ForEach(names, name =>
{
// Do some stuff...
var foo = await GetStuffFrom3rdPartyAsync(name);
// Do some more stuff, with the foo.
});
or is there some gotcha's that I need to be made aware of?
EDIT: No idea if this compiles, btw. Just Pseduo-code .. thinking out loud.
No, It doesn't make sense to combine async with Paralell.Foreach.
Consider the following example:
private void DoSomething()
{
var names = Enumerable.Range(0,10).Select(x=> "Somename" + x);
Parallel.ForEach(names, async(name) =>
{
await Task.Delay(1000);
Console.WriteLine("Name {0} completed",name);
});
Console.WriteLine("Parallel ForEach completed");
}
What output you will expect?
Name Somename3 completed
Name Somename8 completed
Name Somename4 completed
...
Parallel ForEach completed
That's not what will happen. It will output :
Parallel ForEach completed
Name Somename3 completed
Name Somename8 completed
Name Somename4 completed
...
Why? Because when ForEach hits first await the method actually returns, Parallel.ForEach doesn't know it is asynchronous and it ran to completion!. Code after await runs as continuation on another thread not "Paralell processing thread"
Stephen toub addressed this here
From the name, I'm assuming that GetStuffFrom3rdPartyAsync is I/O-bound. The Parallel class is specifically for CPU-bound code.
In the asynchronous world, you can start multiple tasks and then (asynchronously) wait for them all to complete using Task.WhenAll. Since you're starting with a sequence, it's probably easiest to project each element to an asynchronous operation, and then await all of those operations:
await Task.WhenAll(names.Select(async name =>
{
// Do some stuff...
var foo = await GetStuffFrom3rdPartyAsync(name);
// Do some more stuff, with the foo.
}));
A close alternative might be this:
static void ForEach<T>(IEnumerable<T> data, Func<T, Task> func)
{
var tasks = data.Select(item =>
Task.Run(() => func(item)));
Task.WaitAll(tasks.ToArray());
}
// ...
ForEach(names, name => GetStuffFrom3rdPartyAsync(name));
Ideally, you shouldn't be using a blocking call like Task.WaitAll, if you can make the whole chain of methods calls async, "all the way down" on the current call stack:
var tasks = data.Select(item =>
Task.Run(() => func(item)));
await Task.WhenAll(tasks.ToArray());
Furthermore, if you don't do any CPU-bound work inside GetStuffFrom3rdPartyAsync, Task.Run may be redundant:
var tasks = data.Select(item => func(item));
As pointed out by #Sriram Sakthivel there are some problems with using Parallel.ForEach with asynchronous lambdas. Steven Toub's ForEachASync can do the equivalent. He talks about it here, but here is the code:
public static class Extensions
{
public static Task ForEachAsync<T>(this IEnumerable<T> source, int dop, Func<T, Task> body)
{
return Task.WhenAll(
from partition in Partitioner.Create(source).GetPartitions(dop)
select Task.Run(async delegate {
using (partition) while (partition.MoveNext()) await body(partition.Current);
}));
}
}
It uses the Partitioner class to create a load balancing partitioner(doco), and allows you to specify how many threads you want to run with the dop parameter. to see the difference between it and Parallel.ForEach. Try the following code.
class Program
{
public static async Task GetStuffParallelForEach()
{
var data = Enumerable.Range(1, 10);
Parallel.ForEach(data, async i =>
{
await Task.Delay(1000 * i);
Console.WriteLine(i);
});
}
public static async Task GetStuffForEachAsync()
{
var data = Enumerable.Range(1, 10);
await data.ForEachAsync(5, async i =>
{
await Task.Delay(1000 * i);
Console.WriteLine(i);
});
}
static void Main(string[] args)
{
//GetStuffParallelForEach().Wait(); // Finished printed before work is complete
GetStuffForEachAsync().Wait(); // Finished printed after all work is done
Console.WriteLine("Finished");
Console.ReadLine();
}
if you run GetStuffForEachAsync the program waits for all work to finish. If you run GetStuffParallelForEach, the line Finished will be printed before the work is finished.

Simplest way to run three methods in parallel in C#

I have three methods that I call to do some number crunching that are as follows
results.LeftFront.CalcAi();
results.RightFront.CalcAi();
results.RearSuspension.CalcAi(geom, vehDef.Geometry.LTa.TaStiffness, vehDef.Geometry.RTa.TaStiffness);
Each of the functions is independent of each other and can be computed in parallel with no dead locks.
What is the easiest way to compute these in parallel without the containing method finishing until all three are done?
See the TPL documentation. They list this sample:
Parallel.Invoke(() => DoSomeWork(), () => DoSomeOtherWork());
So in your case this should just work:
Parallel.Invoke(
() => results.LeftFront.CalcAi(),
() => results.RightFront.CalcAi(),
() => results.RearSuspension.CalcAi(geom,
vehDef.Geometry.LTa.TaStiffness,
vehDef.Geometry.RTa.TaStiffness));
EDIT: The call returns after all actions have finished executing. Invoke() is does not guarantee that they will indeed run in parallel, nor does it guarantee the order in which the actions execute.
You can do this with tasks too (nicer if you later need Cancellation or something like results)
var task1 = Task.Factory.StartNew(() => results.LeftFront.CalcAi());
var task2 = Task.Factory.StartNew(() => results.RightFront.CalcAi());
var task3 = Task.Factory.StartNew(() =>results.RearSuspension.CalcAi(geom,
vehDef.Geometry.LTa.TaStiffness,
vehDef.Geometry.RTa.TaStiffness));
Task.WaitAll(task1, task2, task3);
In .NET 4, Microsoft introduced the Task Parallel Library which was designed to handle this kind of problem, see Parallel Programming in the .NET Framework.
To run parallel methods which are independent of each other ThreadPool.QueueUserWorkItem can also be used. Here is the sample method-
public static void ExecuteParallel(params Action[] tasks)
{
// Initialize the reset events to keep track of completed threads
ManualResetEvent[] resetEvents = new ManualResetEvent[tasks.Length];
// Launch each method in it's own thread
for (int i = 0; i < tasks.Length; i++)
{
resetEvents[i] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback((object index) =>
{
int taskIndex = (int)index;
// Execute the method
tasks[taskIndex]();
// Tell the calling thread that we're done
resetEvents[taskIndex].Set();
}), i);
}
// Wait for all threads to execute
WaitHandle.WaitAll(resetEvents);
}
More detail about this function can be found here:
http://newapputil.blogspot.in/2016/03/running-parallel-tasks-using.html
var task1 = SomeLongRunningTask();
var task2 = SomeOtherLongRunningTask();
await Task.WhenAll(task1, task2);
The benefit of this over Task.WaitAll is that this will release the thread and await the completion of the two tasks.

Categories