BeginInvoke() 是否运行单独的线程?

Does BeginInvoke() run a separate thread?(BeginInvoke() 是否运行单独的线程?)

本文介绍了BeginInvoke() 是否运行单独的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WPF 应用程序中,我想在非 UI 线程中做一些工作,以避免 UI 无响应.为此,我这样做了:

In my WPF application, I want to do some work in a non-UI thread so as to avoid the UI from become not-responding. For that I did this:

var caller = new AsyncMethodCaller<Pattern>(this.SetPatternType);
caller.BeginInvoke(_patterns, null, null);

委托被定义为,

public delegate void AsyncMethodCaller<in T>(IEnumerable<T> data);

我的问题是:

BeginInvoke() 是否创建一个新线程并且回调SetPatternType 在其中运行?如果是这样,这个帖子会持续多久?

Does BeginInvoke() create a new thread and the callback SetPatternType runs in it? If so, how long this thread last?

这种方法总体上好吗?如果不是,它有什么问题?我可能会面临哪些潜在问题?

Is this approach good in general? If not, what is wrong with it? And what potential problem(s) might I face?

我正在使用 C# 4.0 和 Visual Studio 2010.

I'm using C# 4.0 and Visual Studio 2010.

我还需要一些关于这些的指导:

Also I need few guidelines regarding these:

什么时候我应该自己创建一个新线程,什么时候应该使用 BeginInvoke()?我什么时候应该使用 DispatcherObject.Dispatcher.BeginInvoke() 对象?

When I should create a new thread myself and when should I make use of BeginInvoke()? And when should I use DispatcherObject.Dispatcher.BeginInvoke() object?

推荐答案

它在技术上 不是一个新线程 它是一个 Threadpool 线程,它的持续时间比你的进程/程序长,但可能会运行一些其他线程异步调用它立即完成你的.查看关于 Asynch Programming 和 的 MSDN 文章a href="http://msdn.microsoft.com/en-us/library/3dasc8as(VS.80).aspx" rel="noreferrer">Threadpool 以获取完整的详细信息.

It is technically not a new thread its a Threadpool thread, and it migth last longer than your process/program but might run some other threads asynch calls immediately it finishes yours. Check MSDN articles on Asynch Programming and Threadpool to get the complete details.

根据您的兴趣检查 I/O CompletionPort 了解更多详情.

And depending on your interest check I/O CompletionPort for additional details.

异步编程通常被认为比至少同步代码更好,但如果您使用 .NET 4.0,请查看 任务并行库.

Asynch programming is generally considered better than atleast synchronous code, but f you are on .NET 4.0 take a look at Task Parallel Library.

根据问题编辑,我应该什么时候创建自己的主题?与创建自己的线程相比,使用 BeginInvoke 或 Async 编程总是更好.当您确定需要一个专用线程连续执行某些任务/工作并且您清楚应用程序中多个线程所需的同步机制时,请严格创建自己的线程.除非您有非常令人信服的理由,否则请尽可能避免创建新线程.您今天添加了一个线程,并且可能继续前进,两年后三个开发人员看到为一些连续的东西添加了一个额外的线程,他们会添加更多等等.相信我,我已经看到了这种情况,因此设置正确的做法(即使用异步方法),人们会尝试遵循这一点.我见过有 150 个线程的应用程序,这在双核或四核机器上有意义吗?我不这么认为.

Based on Question Edit, when should I create my own thread? It is always better to use BeginInvoke or Async programming compared to creating your own thread. Strictly create your own thread when you are sure that you need a dedicated thread doing some task/work continuously and you are clear about the synchronization mechanics needed by multiple threads in your application. Avoid creating new threads as long as you can unless you have a really compelling reason. You add a thread today and probably move on and after two years three developers see that an additional thread was added for some continuous stuff, they'll add few more and so on. Trust me I've seen this happening, therefore set the right practices (ie using Asynch methods) and people will try to follow that. I've seen applications with 150 threads, does that make sense on a dual core or quad core machine, I dont think so.

刚刚检查了我的东芝笔记本电脑上所有正在运行的进程中是否存在设计不佳的应用程序,东芝蓝牙管理器使用 53 个线程赢得了我的机器上设计最差的程序的桂冠.:)

Just checked all the running processes on my Toshiba Laptop for such badly designed apps, Toshiba Bluetooth Manager wins the crown of worst designed program on my box using 53 threads. :)

这篇关于BeginInvoke() 是否运行单独的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:BeginInvoke() 是否运行单独的线程?

基础教程推荐