4

I understand that Tasks are just an abstraction for asynchronous execution. Behind the scenes, the runtime still has to develop ways to make our code run asynchronously. I understand there are different implementations of that depending on the type of application running our code: In asp.net, for example, it seems it creates one thread per task with has an HTTP request associated with it. On the other hand, console apps just run tasks in a thread pool (correct if I have the wrong understanding, please).

I understand that those different implementations are the Synchronization context. The changes that I am wrong or too vague are high. I would like to understand the various SynchronizationContext differences between different applications.

I am ashamed to say I have been working with .net for many years, but I still don't understand SynchronizationContext. I also have a feeling that lots of people don't do either. I read tons and tons of articles and official documentation, but nothing clicked. I read most of the articles about the subject, but I still have difficulties with the matter.

Elaine Bel
  • 103
  • 3
  • 1
    Console applications don't usually have a sync-context; nor does modern asp.net; legacy asp.net has two different sync-contexts at different versions and compatibility levels - the old one doesn't work well with tasks, the newer one ... well, it still isn't great, but it is better. But the idea was to help tie the request life-cycle together. Winforms and WPF have a sync-context that pushes things to the relevant UI thread. Honestly, most of the time, most people don't need to know about sync-context. – Marc Gravell Sep 14 '21 at 22:06
  • 1
    "console apps just run tasks in a thread pool" sync-context pre-dates tasks, and there isn't much overlap except for `await` which by default captures the sync-context and tries to use it (if one) to reactivate in an appropriate context – Marc Gravell Sep 14 '21 at 22:08
  • https://stackoverflow.com/a/52687947/17034 – Hans Passant Sep 14 '21 at 23:01

1 Answers1

0

I've been stuck with the same question for quite a while a long time ago. Unfortunately I didn't have a chance to find a definitive answer myself since I didn't have an urge to dig into it -- just my curiosity, so it just sunk in my backlog.

Here're some links that helped me a bit. I'm not sure if you've already encountered them or not, but I hope this helps even a bit:

Three part series of explaining what this thing is and writing an example of your own: https://www.codeproject.com/Articles/31971/Understanding-SynchronizationContext-Part-I, https://www.codeproject.com/Articles/32113/Understanding-SynchronizationContext-Part-II, https://www.codeproject.com/Articles/32119/Understanding-SynchronizationContext-Part-III

I was exploring it in the context of async/await, so maybe this blogpost about that may help you too: https://devblogs.microsoft.com/oldnewthing/20170720-00/?p=96655 (it also contains a useful link inside: https://learn.microsoft.com/en-us/archive/msdn-magazine/2011/october/asynchronous-programming-pause-and-play-with-await).

P. S. I understand that this is barely an answer, so I'd post this in a comment, but the symbol limit would make it a nightmare. I hope someone will post a definitive answer here because I'm interested in the subject too :)