Call long running method and continue with other tasks
.net, async-await, asynchronous, c#
Solution
The simplest solution I know of is:
Task1();
Task2();
var task3 = Task.Run(() => PerformLongTask());
Task4();
Task5();
task3.Wait(); //if task3 has not started yet it will be inlined here
Simple and efficient. If you need to propagate errors you should probably use `Parallel.Invoke`:
Parallel.Invoke(
() => { PerformLongTask(); },
() => { Task4(); Task5(); }
);
Problem
``` public class PerformMainTask() { Task1(); Task2(); PerformLongTask(); Task3(); Task4(); } ``` What I would like to achieve here is to `PerformLongTask()` onto another thread, and to continue with Task3 & Task4 even when `PerformLongTask()` is still running. How should my `PerformLongTask()` be like in a C# 5.0 way? Do I need to use `async/await`?