异步委托VS线程/线程池? [英] Asynchronous Delegates Vs Thread/ThreadPool?

查看:163
本文介绍了异步委托VS线程/线程池?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行3并行任务和每个任务完成后,他们应该调用哪个打印出的结果相同的功能。

I need to execute 3 parallel tasks and after completion of each task they should call the same function which prints out the results.

我不使用.NET明白为什么我们有呼叫台异步(delegate.BeginInvoke()及delegate.EndInvoke())?以及Thread类

I don't understand in .net why we have Asychronous calling (delegate.BeginInvoke() & delegate.EndInvoke()) as well as Thread class?

我有点糊涂了使用哪一个的时候?现在,在这种特殊情况下,我该台异步使用主叫或Thread类?

I'm little confused which one to use when? Now in this particular case, what should I use Asychronous calling or Thread class?

我使用C#。

推荐答案

1。异步委托

呼叫台异步时可以使用   具有应进行处理的工作项目   在后台的你照顾的时候   他们完成的。

Asychronous calling is used when you have work items that should be handled in the background and you care when they finish.

<一个href="http://stackoverflow.com/questions/1506838/backgroundworker-vs-background-thread/1507337#1507337">http://stackoverflow.com/questions/1506838/backgroundworker-vs-background-thread/1507337#1507337

2。 BackgroundWorker的

      
  • 使用的BackgroundWorker 如果您有运行在一个单一的任务   背景的需要与互动   用户界面。的,并使用它,如果的你没有当他们完成的他们的任务在意。编组数据的任务   和方法调用到UI线程是   通过自动处理的   基于事件的模型。
  •   
  • 避免BackgroundWorker的如果(1)程序集已不   引用System.Windows.Form   装配,(2),你需要线程   是前台线程,或(3)你   需要操纵线程   优先级。
  •   
  • Use BackgroundWorker if you have a single task that runs in the background and needs to interact with the UI. and use it if you don't care when they finish their task. The task of marshalling data and method calls to the UI thread are handled automatically through its event-based model.
  • Avoid BackgroundWorker if (1) your assembly does not already reference the System.Windows.Form assembly, (2) you need the thread to be a foreground thread, or (3) you need to manipulate the thread priority.

3。线程池

      
  • 使用线程池螺纹时,效率是需要的。线程池   有助于避免相关的开销   与创建,启动和停止   线程。
  •   
  • 避免使用线程池如果(1)任务运行的的寿命您   应用程序,(2),你需要的线程   是一个前台线程,(3),则   需要操纵线程   优先级,或(4)您需要的线程   有一个固定的身份(中止,   悬浮,发现)。
  •   
  • Use a ThreadPool thread when efficiency is desired. The ThreadPool helps avoid the overhead associated with creating, starting, and stopping threads.
  • Avoid using the ThreadPool if (1) the task runs for the lifetime of your application, (2) you need the thread to be a foreground thread, (3) you need to manipulate the thread priority, or (4) you need the thread to have a fixed identity (aborting, suspending, discovering).

4。 Thread类

      
  • 使用线程的长时间运行的任务类,当你   需要通过一个正式提供的功能   线程模型,例如,选择   前景和背景之间   线程,调整线程的优先级,   细粒度的控制线   执行等。
  •   
  • Use the Thread class for long-running tasks and when you require features offered by a formal threading model, e.g., choosing between foreground and background threads, tweaking the thread priority, fine-grained control over thread execution, etc.

这篇关于异步委托VS线程/线程池?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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