简体   繁体   中英

Understanding context in .Net Task execution

I have been trying to understand the concept of context in Task execution in .Net. However, I still am unable to relate context to basic concept of OS threads. While going through this blog , I sort of got the following idea of what a context is:

In GUI applications, there is a single GUI thread which has all the GUI elements. Now, because one needs to come in the GUI thread to access the GUI elements, I am assuming the GUI thread has the GUI elements initialized in its stack space which it doesn't share with other threads. Therefore, the awaitable needs to schedule the remainder of the function in the GUI thread if the remainder function wants to access some GUI element. Similarly, if we talk about HTTP application which accept HTTP get/post requests, there is a thread which gets spawned when a request arrives. This thread contains the request context like IP address of the user. Now, if the remainder function wants to access some HTTP context properties, it has to execute in that thread.

While reading this blog , I came across the idea of context being copied . This has led to me believe the following:

The context of a thread is data members like IP address, GUI elements etc. When the remainder of a function is scheduled after an awaitable completes, the remainder may need the context to be present, but not necessarily on the same thread. So, what is done is any thread is taken out of the thread pool and the context is copied onto that thread so that it is accessible. Thereafter, the remainder function is scheduled on this thread. This can cause a deadlock in the following way. Take GUI applications for example. At any time, there should be a unique thread having the GUI context. So, if the GUI thread blocks and doesn't release the context, the remainder function won't get scheduled.

Can anyone please clarify this for me? What exactly is in a context? And how is the context transferred? Which of the above understanding of mine is right or both are wrong?


UPDATE: I read this blog and it has a line And this extension method demonstrates how to invoke a function with a specified ExecutionContext (typically, captured from another thread) . This is pushing me to believe that my second idea is closer to correctness.

Each context is different. But in general, they're not copied . Contexts are used to schedule Tasks . That is, to find a suitable thread and other resources, as required, and to then execute the task.

In some contexts (GUI), the most important thing is the thread. There's one UI thread, so any Task that a GUI context is asked to schedule has to arrange for the UI thread to execute that Task .

In some contexts (ASP.Net before core), what's important are the "ambient" request/response/session objects. Those objects should only be accessed by a single thread at a time, but any thread can be used. So the context can use thread pool threads but needs to ensure it only executes a single Task at a time.

And in the default context, there's no special thread nor any other special resources. Like the ASP.Net context above, any thread pool thread can be used to execute the Task but it can schedule Task s as quickly as the thread pool will take them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM