-1

I have the following code:

protected override async void OnStart() {
   await Helper.PopulateMetrics();
   await Helper.LogStart();
   if (Settings.Rev == REV.No && (new[] { 15, 30, 50 }).Contains(Settings.Trk2))

I guess I am confused as when I set a breakpoint in await Helper.LogStart() I see that breakpoint his before the line starting with "if (Settings ... "

as there is an await should the code after those not be hit first?

Here's what the LogStart() method looks like:

 public static async Task LogStart()
 {
     // code
     await App.CDB.InsertLogItem(logStart);
 }

Ideally I would like these two methods to just run in the background one after the other while the code immediately skips them.

Alan2
  • 23,493
  • 79
  • 256
  • 450
  • 2
    Well because you are specifically `await` *ing* it, nothing will continue at that point until it's finished. – Trevor Feb 21 '20 at 14:40
  • 2
    You seem to completely misunderstood what `await` is. The lines after `await` will run after the thing being awaited is completed. – Sweeper Feb 21 '20 at 14:40
  • Nope, you are `await`ing the result of the call before proceeding. If you had omitted the `await` then it's possible the the `if` could be executed before `await App.CDB.InsertLogItem(logStart);` – phuzi Feb 21 '20 at 14:40
  • 4
    Does this answer your question? [How does await async work in C#](https://stackoverflow.com/questions/17488677/how-does-await-async-work-in-c-sharp) – r3dst0rm Feb 21 '20 at 14:41
  • App.CDB.InsertLogItem(logStart); is async and it's suggesting I add an await – Alan2 Feb 21 '20 at 14:46
  • 1
    As a side note [avoid async void](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming#avoid-async-void). – Theodor Zoulias Feb 21 '20 at 16:31

2 Answers2

2

The await keyword causes the execution to wait until the Helper.LogStart() function has completed. If you want to continue executing, you can store the returned Task object into another variable and await on it later:

var task = Helper.LogStart();
/* something else */
await task;
janw
  • 8,758
  • 11
  • 40
  • 62
  • What I want is to skip the lengthy background jobs and finish the onStart as soon as possible. Can you give an example of how I could do that. – Alan2 Feb 21 '20 at 14:43
  • Do you need to keep track of the background jobs, or would it suffice to use a fire&forget approach? In this case, you might just skip the `await` at all. However, I recommend to at least specify a `.ContinueWith()` in case an exception occurs. – janw Feb 21 '20 at 14:46
  • @Alan2 `OnStart` does return as soon as you hit `await`. But when the task finishes, you kind of "come back to" `OnStart`. I see that you are familiar with JavaScript, so maybe I should explain this in terms of promises: The lines after the `await` is basically the `then` part of a Promise. – Sweeper Feb 21 '20 at 14:47
  • Fire and forget for the other two tasks. But I am interested to know how I could implement a .ContinueWith() also. – Alan2 Feb 21 '20 at 14:49
  • 1
    @Alan2 Async/await is not meant to be a fire and forget type of thing. Depending on the type of app you're doing, there are other methods to schedule background work to be done in a fire and forget manner. – mason Feb 21 '20 at 14:50
  • @mason - okay thanks. I see now. Do you have time to add a suggestions / explanation as an answer and I can accept so others can also see your suggestion. – Alan2 Feb 21 '20 at 14:54
  • @Alan2 I agree with @mason, this is likely to cause problems at some point (I learned myself the hard way). We would need to know the kind of framework you are using to suggest better approaches. For the `.ContinueWith()`, refer to [this answer](https://stackoverflow.com/a/47570595/8528014) or [this one](https://stackoverflow.com/a/27852439/8528014). – janw Feb 21 '20 at 14:54
1

When we use await that does not means it skip that code and run in background. When we have await at that place new child thread would be created and that new thread will handle execution of PopularMatrics() but Main thread will wait until child thread finished its job.

  • Async-await has nothing to do with threads. It just tells that execution will continue when the awaited method finishes. – Alejandro Feb 21 '20 at 14:47
  • thread doesn't block while tasks are running. The await keyword provides a non-blocking way to start a task, then continue execution when that task completes. – Jignesh Patel Feb 21 '20 at 14:58
  • Correct, it doesn't blocks the current thread, it's released for doing other things in the meanwhile, but it does NOT create a new thread for running the awaited method (not necesarily). It only means that it will run "some time after", how and when only depends on how the method is implemented. – Alejandro Feb 21 '20 at 15:59