What are the SynchronizationContext differences between Console, Windows Forms, WPF?

Viewed 73

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.

1 Answers

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://docs.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 :)

Related