BeginInvoke()运行一个单独的线程? [英] Does BeginInvoke() run a separate thread?

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

问题描述

在我的WPF应用程序中,我想在非UI线程中做一些工作,以避免UI变得不响应。为此,我做到这一点:

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

代理定义为

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

我的问题是:



code> BeginInvoke()创建一个新线程,回调 SetPatternType 在其中运行?如果是这样,这个线程持续了多长时间?



这种方法一般都很好吗?如果没有,怎么了?我可能面临什么潜在的问题?



我使用C#4.0和Visual Studio 2010。



< hr>

编辑:



此外,我几乎没有关于这些的指导原则:



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

解决方案

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



根据您的兴趣查看 I / O CompletionPort 获取更多详细信息。



Asynch编程通常被认为比至少同步代码更好,但是你在.NET 4.0中看看任务并行库



根据问题编辑,创建自己的线程?
与创建自己的线程相比,使用BeginInvoke或Async编程总是更好。严格创建自己的线程,当你确定你需要一个专门的线程做一些任务/工作不间断,你清楚你的应用程序中多个线程所需的同步机制。除非你有一个真正令人信服的理由,否则只要你可以创建新的线程。你今天添加一个线程,可能在两年之后,三位开发人员看到,为一些连续的东西添加了一个额外的线程,他们会添加更多等等。相信我已经看到这种情况,因此设置正确的做法(即使用Asynch方法),人们会尝试遵循。我已经看到150个线程的应用程序,这对双核或四核机器是有意义的,我不这么认为。



只需检查我的所有正在运行的进程东芝笔记本电脑为这种设计不当的应用程序,东芝蓝牙管理器赢得了最坏的设计程序的冠在我的盒子使用53线程。 :)


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);

And the delegate is defined as,

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

My question is:

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?

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


EDIT:

Also I need few guidelines regarding these:

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?

解决方案

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.

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

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.

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.

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()运行一个单独的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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