gpt4 book ai didi

wpf - WPF 应用程序的 19 个线程

转载 作者:行者123 更新时间:2023-12-01 23:56:11 26 4
gpt4 key购买 nike

我刚刚创建了一个新的 WPF 应用程序,它有大约 5 个窗口并使用 Xceed 数据网格。查看任务管理器后,我发现这个小应用程序有 19 个线程正在运行。

这是正确的吗?我预计 WPF 框架使用 2-3 个线程,但不是 19 个。还是我做错了什么?

最佳答案

首先,它不是 19。

运行“Hello, World!”时WPF 应用程序,我在任务管理器中看到 10 个线程。使用 Visual Studio 调试器运行相同的应用程序时,它使用 19。因此,首先,从调试器中提取出其中作为开销添加的 9 个。

现在是 10

根据 MSDN 杂志文章 "Build More Responsive Apps With The Dispatcher" ,WPF 以两个线程开始:

All WPF applications start out with two important threads, one for rendering and one for managing the user interface. The rendering thread is a hidden thread that runs in the background, so the only thread that you ordinarily deal with is the UI thread.

但是根据this article ,所有托管应用程序中都有很多基本线程,无论是否为 WPF:

There are different types of .NET threads: finalizer, GC, debugger, timer, threadpool, primary and background threads. But not all threads related to .NET are managed.

从 WPF 中的两个开始,添加 GC 和终结器以达到四个。添加调试器线程(所有托管应用程序即使在不调试时也有一个)以达到五个。添加计时器以达到

并且,在 VS 外部启动 WPF 应用程序后,将调试器附加到它,我可以看到线程池中有两个工作线程。这可能是最低限度。这使我的应用程序达到八个

有两个我无法准确解释,但它们很可能是用于互操作的 COM 线程(它们绝对不是托管线程)。 second article我引用了更多相关讨论。

但这个故事的真正寓意是,不要担心它们。如果是 Vanilla “你好,世界!”应用程序使用 10,这是您的基线。 只需担心您创建的线程超出此最小值。

关于wpf - WPF 应用程序的 19 个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1484060/

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