0

For UI application, we know that there is a SynchronizationContext which store main thread context in order make continuation to be ran in the main thread (by .ConfigureAwait(true)). However, SynchronizationContext is absent in console application, then how can I keep the continuation to be ran in main thread?

mannok
  • 1,712
  • 1
  • 20
  • 30
  • 1
    Why do you want to do this? – mjwills Feb 22 '21 at 06:15
  • If you need to run a method synchronously then you don't need use ``Task``! – sa-es-ir Feb 22 '21 at 06:17
  • @mjwills One of my 3rd party library is keep checking for the thread id. If the thread id is not the same with the one in main thread. It will throw license related exception. – mannok Feb 22 '21 at 06:19
  • @SaeedEsmaeelinejad I does not meant to run it synchronously. I just want the continuation to be ran synchronously – mannok Feb 22 '21 at 06:20
  • 1
    You could use the `AsyncContext` class from the [Nito.AsyncEx.Context](https://www.nuget.org/packages/Nito.AsyncEx.Context/) package ([documentation](http://dotnetapis.com/pkg/Nito.AsyncEx.Context/5.0.0/netstandard2.0/doc/Nito.AsyncEx.AsyncContext)). You'll have to put all your code inside an `AsyncContext.Run(async () => { /* ... */ })` call. It's a blocking call, much like the analogous `Application.Run(new Form1());`. – Theodor Zoulias Feb 22 '21 at 06:23
  • @TheodorZoulias no native solution to achieve this? – mannok Feb 22 '21 at 06:27
  • mannok AFAIK no native API exists. The most native-ish you can get is to use the `AsyncPump` class from [this](https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/ "Await, SynchronizationContext, and Console Apps") blog post. – Theodor Zoulias Feb 22 '21 at 06:34
  • Note that AsyncContext actually just runs on a single threaded custom task scheduler. Which may not be what you want – TheGeneral Feb 22 '21 at 07:00
  • 1
    You can also check this publication https://devblogs.microsoft.com/pfxteam/await-synchronizationcontext-and-console-apps/, which describes how to create your own `SingleThreadSynchronizationContext` – Serg Feb 22 '21 at 07:01
  • 1
    @00110001 why do you think that the OP may not want single-threaded scheduling? They specifically ask for the `await` *"continuation to be ran in the main thread"*. Which is exactly what the `AsyncContext` does (provided that you don't exit the context with `ConfigureAwait(false)`). – Theodor Zoulias Feb 22 '21 at 07:16
  • You could likely just get away with `Wait`, or `Result`or `.GetAwaiter().GetResult()`. – TheGeneral Feb 22 '21 at 07:29
  • 2
    mannok there is also the [`SingleThreadedSynchronizationContext`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.threading.singlethreadedsynchronizationcontext) class, from the [Microsoft.VisualStudio.Threading](https://www.nuget.org/packages/Microsoft.VisualStudio.Threading/) package. The documentation about how to use this class is non existent. The whole package is a potpourri of heterogeneous APIs. It installs all kinds of extensions methods to native classes, polluting the intellisense with APIs that you'll probably never use. So it's not a package I would recommend. – Theodor Zoulias Feb 22 '21 at 10:43

0 Answers0