SynchronizationContext.Current 在主线程上为空
SynchronizationContext.Current is null on Main thread
SynchronizationContext.Current
在主线程上为空,我很难弄清楚这一点。
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
}
}
我 运行 使用 .NET 4.6.1。它是一个混合的 Winforms 和 WPF 应用程序。入口点是 WinForms。以下是此类证据的一些屏幕截图:
它也与 this 之类的帖子无关,因为我使用的是较新的 .NET 版本,而且似乎提到的问题已得到修补。还有其他好的想法吗?
当您的应用程序的入口点 (Main
) 被命中时,当然没有 SynchronizationContext
可用。您需要等待框架对其进行初始化。
在 Windows 表单应用程序中,这会在创建第一个表单时发生。所以只有在 Application.Run
方法被调用之后,SynchronizationContext.Current
属性 实际上 return 一个同步上下文。
SynchronizationContext.Current
在主线程上为空,我很难弄清楚这一点。
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
}
}
我 运行 使用 .NET 4.6.1。它是一个混合的 Winforms 和 WPF 应用程序。入口点是 WinForms。以下是此类证据的一些屏幕截图:
它也与 this 之类的帖子无关,因为我使用的是较新的 .NET 版本,而且似乎提到的问题已得到修补。还有其他好的想法吗?
当您的应用程序的入口点 (Main
) 被命中时,当然没有 SynchronizationContext
可用。您需要等待框架对其进行初始化。
在 Windows 表单应用程序中,这会在创建第一个表单时发生。所以只有在 Application.Run
方法被调用之后,SynchronizationContext.Current
属性 实际上 return 一个同步上下文。