gpt4 book ai didi

c# - SynchronizationContext.Current 在主线程上为空

转载 作者:行者123 更新时间:2023-11-30 21:39:42 30 4
gpt4 key购买 nike

SynchronizationContext.Current 在主线程上为 null,我很难弄清楚这一点。

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。以下是此类证据的一些屏幕截图:

enter image description here enter image description here

它也与 this 等帖子无关因为我使用的是较新的 .NET 版本,而且似乎提到的问题已经修补。还有其他好的想法吗?

最佳答案

当您的应用程序的入口点 (Main) 被命中时,当然没有可用的 SynchronizationContext。您需要等待框架对其进行初始化。

在 Windows 窗体应用程序中,这会在创建第一个窗体时发生。因此只有在 Application.Run 方法被调用后,SynchronizationContext.Current 属性才会真正返回一个同步上下文。

关于c# - SynchronizationContext.Current 在主线程上为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45054201/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com