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

时间:2022-08-27 20:51:43

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:

在我的WPF应用程序中,我希望在非UI线程中做一些工作,以避免UI不再响应。为此,我这样做:

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?

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

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.

我使用c# 4.0和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?

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

3 个解决方案

#1


11  

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.

从技术上讲,它不是一个新的线程,它是一个Threadpool线程,并且它比进程/程序持续的时间更长,但是可能会在它完成您的进程/程序之后立即运行其他一些线程异步调用。检查MSDN关于异步编程和Threadpool的文章以获得完整的细节。

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

并根据您的兴趣,检查I/O完成端口以获得更多细节。

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.

异步编程通常被认为比最少的同步代码更好,但是如果你在。net 4.0中,你可以看看任务并行库。

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.

基于问题编辑,何时应该创建自己的线程?与创建自己的线程相比,使用BeginInvoke或Async编程总是更好的。当您确信需要一个专门的线程连续执行某些任务/工作时,并且清楚应用程序中多个线程所需的同步机制时,请严格地创建您自己的线程。尽量避免创建新线程,除非你有一个非常引人注目的理由。你今天添加了一个线程,可能会继续,两年后,三名开发人员看到一个额外的线程被添加到一些连续的东西中,他们会添加更多的线程,等等。相信我,我已经看到了这种情况的发生,因此设置了正确的实践(即使用异步方法),人们将尝试遵循它。我已经见过有150个线程的应用程序,这在双核心或quad核心机器上是有意义的,我不这么认为。

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. :)

我刚刚检查了我的东芝笔记本电脑上的所有运行程序,这些程序设计得非常糟糕,东芝蓝牙管理器在我的盒子上用53个线程赢得了最差设计程序的桂冠。:)

#2


8  

It uses the thread pool - so it won't necessarily create a new thread, but it runs in a different thread to the calling thread (unless that's a thread pool thread itself which happens to finish its task before the delegate invocation is scheduled; it would be pretty unlikely to use the same thread).

它使用线程池——因此它并不一定会创建一个新线程,但是它在一个不同的线程中运行到调用线程(除非是线程池线程本身,在调度调用之前完成任务);它不太可能使用相同的线程)。

#3


3  

Dispatcher.CurrentDispatcher is new stuff in WPF (kind of replaces InvokeRequired stuff in WinForms).

调度员。CurrentDispatcher是WPF中的一个新东西(用WinForms代替InvokeRequired)。

You can use Dispatcher to queue any updates you want to the GUI and it has different priorities you can choose from

您可以使用Dispatcher来排队任何您想要对GUI进行的更新,并且它有不同的优先级,您可以从中选择。

see this MSDN link

看到这个MSDN链接

#1


11  

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.

从技术上讲,它不是一个新的线程,它是一个Threadpool线程,并且它比进程/程序持续的时间更长,但是可能会在它完成您的进程/程序之后立即运行其他一些线程异步调用。检查MSDN关于异步编程和Threadpool的文章以获得完整的细节。

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

并根据您的兴趣,检查I/O完成端口以获得更多细节。

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.

异步编程通常被认为比最少的同步代码更好,但是如果你在。net 4.0中,你可以看看任务并行库。

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.

基于问题编辑,何时应该创建自己的线程?与创建自己的线程相比,使用BeginInvoke或Async编程总是更好的。当您确信需要一个专门的线程连续执行某些任务/工作时,并且清楚应用程序中多个线程所需的同步机制时,请严格地创建您自己的线程。尽量避免创建新线程,除非你有一个非常引人注目的理由。你今天添加了一个线程,可能会继续,两年后,三名开发人员看到一个额外的线程被添加到一些连续的东西中,他们会添加更多的线程,等等。相信我,我已经看到了这种情况的发生,因此设置了正确的实践(即使用异步方法),人们将尝试遵循它。我已经见过有150个线程的应用程序,这在双核心或quad核心机器上是有意义的,我不这么认为。

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. :)

我刚刚检查了我的东芝笔记本电脑上的所有运行程序,这些程序设计得非常糟糕,东芝蓝牙管理器在我的盒子上用53个线程赢得了最差设计程序的桂冠。:)

#2


8  

It uses the thread pool - so it won't necessarily create a new thread, but it runs in a different thread to the calling thread (unless that's a thread pool thread itself which happens to finish its task before the delegate invocation is scheduled; it would be pretty unlikely to use the same thread).

它使用线程池——因此它并不一定会创建一个新线程,但是它在一个不同的线程中运行到调用线程(除非是线程池线程本身,在调度调用之前完成任务);它不太可能使用相同的线程)。

#3


3  

Dispatcher.CurrentDispatcher is new stuff in WPF (kind of replaces InvokeRequired stuff in WinForms).

调度员。CurrentDispatcher是WPF中的一个新东西(用WinForms代替InvokeRequired)。

You can use Dispatcher to queue any updates you want to the GUI and it has different priorities you can choose from

您可以使用Dispatcher来排队任何您想要对GUI进行的更新,并且它有不同的优先级,您可以从中选择。

see this MSDN link

看到这个MSDN链接