Reputation: 4895
SynchronizationContext.Current
is null on Main thread and I have had a hard time figuring this one out.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
var ctx = System.Threading.SynchronizationContext.Current;
// ctx is null here
}
}
I am running on .NET 4.6.1. It is a mixed Winforms and WPF app. The entry point is WinForms. Here are some screenshots of such evidence:
It is also not related to posts like this as I am using newer .NET version and seems mentioned issue was patched already. Any other good ideas?
Upvotes: 4
Views: 1986
Reputation: 169390
Of course there is no SynchronizationContext
available when the entry point (Main
) of your application is hit. You need to wait for the framework to initialize it.
In a Windows Forms application this happens when the first form is created. So only after the Application.Run
method has been called, the SynchronizationContext.Current
property will actually return a synchronization context.
Upvotes: 4