Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does 'context' exactly mean in C# async/await code?

Lets looks at some simple C# async/await code where I have an object reference (obj) before and after an await with ConfigureAwait(false)

private async Task<SomeObject> AnAsyncLibraryMethod(SomeObject obj) {     Console.WriteLine(Thread.CurrentThread.ManagedThreadId);     obj.Name = "Harry"; // <-- obj here      // MAIN POINT     var newSubObj = await FetchOverHttpAsync().ConfigureAwait(false);      // Continuation here     Console.WriteLine(Thread.CurrentThread.ManagedThreadId);     obj.Name = "Sally"; // <-- same obj here     return obj; }  public class SomeObject { public string Name; } 

ConfigureAwait(false) seems to means NOT marshal the continuation back to the original context captured - ok, but what does that really mean? I've tried the code above and obj IS correctly referenced back (even when it resumes on a different thread).

So the "context" doesn't appear to be the thread's working memory (i.e. thread local storage). So what does "context" contain? Therefore, what does it really mean to

marshal the continuation back to the original context captured

like image 385
DeepSpace101 Avatar asked Mar 25 '15 22:03

DeepSpace101


People also ask

What does a context means?

Definition of context 1 : the parts of a discourse that surround a word or passage and can throw light on its meaning. 2 : the interrelated conditions in which something exists or occurs : environment, setting the historical context of the war.

What is a context object in programming?

A context object encapsulates the references/pointers to services and configuration information used/needed by other objects. It allows the objects living within a context to see the outside world.

What is contextual structure in programming?

Contextual structure (also called static semantics) defines the program semantics before dynamic execution.

What is context stackoverflow?

In english, if we refer to context, we refer to information surrounding something that allows you to understand the entire situation in which that something exists. For example, we may say a statement is "taken out of context".


2 Answers

If I'm not totally wrong, ConfigureAwait(false); only means that the code which runs after the code you are awaiting, is not required to use the SynchronizationContext from before the await.

SynchronizationContext can be different things as Stephen pointed out. So imagine you are in a web environment and your code after the await relies on HttpContext.Current.Items, this might not work anymore if you set ConfigureAwait(false);

The following code in an MVC controller would throw an exception for example

    public async Task<ActionResult> Index()     {         System.Web.HttpContext.Current.Items["test"] = "test";          var result = await SomethingAsync();          return View();     }      private async Task<object> SomethingAsync()     {         await Task.Delay(1000).ConfigureAwait(false);          // this will throw a nullpointer if ConfigureAwait is set to false         return System.Web.HttpContext.Current.Items["test"];     } 

Your variable though is simply in the scope of the method and therefor it will be available, basically the method closure/scope, if this makes sense?

like image 199
MichaC Avatar answered Sep 25 '22 03:09

MichaC


As I describe in my async intro blog post, the "context" is:

  • SynchronizationContext.Current, unless it is null, in which case it is
  • TaskScheduler.Current. Note that if there is no current task scheduler, then TaskScheduler.Current is the same as TaskScheduler.Default, which is a thread pool context.

The vast majority of the time, this is either a UI or ASP.NET request context (both types of SynchronizationContext), or else it's the thread pool context. Task scheduler contexts seldomly come into play.

Note that this context is just used to schedule the continuation. It doesn't do anything with marshaling; in your example obj is captured just like it would be if it were referenced from a lambda expression.

like image 40
Stephen Cleary Avatar answered Sep 23 '22 03:09

Stephen Cleary