Executing code when a C# async method returns due to "await"

Mark Lauritsen 1 Reputation point
2023-01-10T16:43:37.27+00:00

tldr: Is there a way to execute some code when an "await" causes a method call to return?

Suppose I log entry and exit of methods with an object whose Dispose() method logs the method's exit. For example

void DoWhatever() { using (LogMethodCall("DoWhatever") { // do whatever } }

That is, the method LogMethodCall() logs "DoWhatever entered" and then returns an object of type CallEnder whose Dispose() method logs "DoWhatever exiting". That works fine until await is used. For example...

async Task DoWhatever() { using (LogMethodCall("DoWhatever") { // do first part. await Something(); // do second part. } }

The above code returns a Task to the caller when it hits the await, and the rest of the code (including the call to CallEnder.Dispose()) runs in that Task. My problem is that I want to log "DoWhatever exiting" when the await triggers the actual return, and not when CallEnder.Dispose() is finally called.

Is there a way to do that? Is there something like an event that's raised when await causes DoWhatever() to return? Maybe something to do with ExecutionContext or CallContext or TaskScheduler?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,991 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 28,196 Reputation points
    2023-01-10T16:53:59.757+00:00

    that is what is happening. await is just a syntax sugar for completion callback.

    the code:

    async Task DoWhatever()
    {
        using (LogMethodCall("DoWhatever")
        {
              await Something();
        }
    }
    

    is the same as:

    Task DoWhatever()
    {
         var log = LogMethodCall("DoWhatever")
         var task = Something();
         task.ContinueWith(() => log.Dispose());     
         return task;
    }
    

    the Dispose will be called when the task Something completes.

    No comments