Can you briefly explain me why the first two rows of this code are not running parallel? How could I make it work paralell?
SensorLeft and SensorRight are of the same class, and Distance is a public property of it which needs some time to be calculated when calling its get method.
What am I doing wrong? Should I make the Distance calculation as an async function instead to be right?
public async void GetDistance()
{
await Task.Run(() => LeftDistance = SensorLeft.Distance);
await Task.Run(() => RightDistance = SensorRight.Distance);
Distance = RightDistance < LeftDistance ? RightDistance:LeftDistance;
}
awaitthe first task you are waiting for it to finish before moving to the next line of code. Assign the tasks first then useTask.WhenAllto let them run at the same time.async voidis only meant for event handlers.You should useasync Task.awaitmeans await for an asynchronous operation to finish. It'sTask.Runthat starts work in parallel, notawait. If you want the operations to run in parallel store the returned tasks in an array and useawait Task.WhenAll(tasks)to wait for both of them to finish.