0

I have an application where I need to do two I/O tasks: get a GPS location, retrieve some data over Bluetooth. Both tasks can vary in time to complete, and can take many seconds. To improve the speed of the application I want to kick-off both activities at the same time, and then wait for both to complete before proceeding.

If I do this:

bool locationStatus= await GetCurrentLocation();
bool vehicleStatus = await GetVehicleData();

does the second function get kicked off, or is it only the UI that is not blocked?

Do I need to invoke parallel tasks instead?

Jerodev
  • 32,252
  • 11
  • 87
  • 108
gregm
  • 341
  • 3
  • 12
  • 2
    There was a duplicate question a few hours ago. There are a *lot* of similar questions with great explanations. Please search for a solution *first* before posting. – Panagiotis Kanavos Jan 26 '18 at 10:41
  • I know it is not the fast answer you seek, but I believe you should invest in studying this. https://gist.github.com/staltz/868e7e9bc2a7b8c1f754 – itdoesntwork Jan 26 '18 at 10:42
  • 1
    By asking for something that's already been answered 100 times you *won't* get good answers - people become tired of answering the same thing after a while. Instead of the great explanation they wrote 1 year ago they'll only post a short answer – Panagiotis Kanavos Jan 26 '18 at 10:42
  • @itdoesntwork that link is unrelated to the question. Rx isn't meant for *asynchronous* programming anyway, it's meant for event stream processing, even if *other* languages like Java use it this way because they lack `async/await` support – Panagiotis Kanavos Jan 26 '18 at 10:43

1 Answers1

6

You are directly awaiting the first function. If you want to let the functions complete asynchronous, you will have to put the await at the position where you need the variables.

For example:

var locationStatus = GetCurrentLocation();
var vehicleStatus = GetVehicleData();

if (await locationStatus && await vehicleStatus)
{
    // Do stuff
}
Jerodev
  • 32,252
  • 11
  • 87
  • 108