WPF 应用程序的 19 个线程 [英] 19 Threads for WPF Application

查看:23
本文介绍了WPF 应用程序的 19 个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just created a new WPF application which has about 5 windows and makes use of the Xceed Data Grid. After looking in the task manager I saw that this small application had 19 threads running.

这是正确的吗?我希望 WPF 框架使用 2-3 个线程,而不是 19 个.还是我做错了什么?

Can this be correct? I expected the WPF framework to use 2-3 threads, but not 19. Or am I doing anything wrong?

推荐答案

首先,它不是 19.

运行Hello, World!"时WPF 应用程序,我在任务管理器中看到 10 个线程.当使用 Visual Studio 调试器运行相同的应用时,它使用 19.因此,首先,将其中 9 个作为调试器的开销添加进来.

When running a "Hello, World!" WPF app, I see 10 threads in Task Manager. When running the same app using the Visual Studio debugger, it uses 19. So, first, factor out 9 of these added as overhead from the debugger.

现在是 10.

根据名为 使用调度程序构建更具响应性的应用程序"的 MSDN 杂志文章,WPF从两个线程开始:

According to a MSDN Magazine article called "Build More Responsive Apps With The Dispatcher", WPF starts out with two threads:

所有 WPF 应用程序都以两个重要的线程,一个用于渲染一个用于管理用户界面.渲染线程是一个隐藏的线程,运行在背景,所以唯一的线程你通常处理的是UI线程.

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.

但根据 这篇文章,所有托管应用程序中都有很多基本线程,WPF 与否:

But according to this article, there are lots of basic threads in all managed apps, WPF or not:

有不同类型的 .NET线程:终结器、GC、调试器、计时器、线程池、主要和后台线程.但不是所有的与 .NET 相关的线程被管理.

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 和终结器以达到 四个.添加调试器线程(所有托管应用程序都有一个,即使没有调试)以达到 五个.添加计时器以达到.

Starting with the two from WPF, add in the GC and the finalizer to get up to four. Add in the debugger thread (all managed apps have one even when not debugging) to get to five. Add the timer to get to six.

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

And, attaching a debugger to my WPF app after starting it outside VS, I can see there are two worker threads in the thread pool. This is likely a minimum. This gets my app up to eight.

有两个我无法确切说明,但它们很可能是用于互操作的 COM 线程(它们绝对不是托管线程).我参考的第二篇文章讨论了更多这些内容.

There are two I cannot account for exactly, but they are likely COM threads for interop (they are definitely not managed threads). The second article I referenced discusses more of these.

但这里故事的真正寓意是,别担心他们.如果一个香草你好,世界!"app 使用 10,这是你的基线.只需担心您创建的线程超出此最小值.

But the real moral of the story here is, don't worry about them. If a vanilla "Hello, World!" app uses 10, that is your baseline. Only worry about threads you create beyond this minimum.

这篇关于WPF 应用程序的 19 个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆