本文介绍了主题/线程池或BackgroundWorker的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道用什么需要很多的表演任务。
的BackgroundWorker 线程池

I would like to know what to use for tasks that need alot of performance.Backgroundworker, Thread or ThreadPool?

我一直使用线程,到目前为止,但我需要提高我的应用程序的速度。

I've been working with Threads so far, but I need to improve speed of my applications.

推荐答案

BackgroundWorker的是同样的事情作为一个线程池线程。它的补充的运行在UI线程事件的能力。非常有用的,以显示进展,并使用该结果和更新的用户界面。因此,它的典型用法是prevent的UI冻结作品时需要做。这是其运行code的的能力有用的异步的,性能是不是第一个进球。这种模式也被后面的.NET版本中的任务和巧妙地延长LT;>类和异步/等待关键字

BackgroundWorker is the same thing as a thread pool thread. It adds the ability to run events on the UI thread. Very useful to show progress and to use the result and update the UI. So its typical usage is to prevent the UI from freezing when works needs to be done. It is useful for its ability to run code asynchronously, performance is not the first goal. This pattern is also ably extended in later .NET versions by the Task<> class and the async/await keywords.

线程池中的线程都是以避免消耗的资源非常有用。线程是一种昂贵的操作系统对象,你可以创建它们的数量非常有限。一个线程需要5操作系统处理和虚拟内存地址空间的兆字节。没有Dispose()方法来提前释放这些句柄。线程池中的线程可以让你的再利用的线程。你使用一个线程池线程只有当它的工作是有限的,最好不要服用超过半秒是很重要的。而不是经常堵。因此,最适合于短时间的工作,没有什么在性能方面。处理I / O完成是一个TP线程的理想的工作。

Thread pool threads are useful to avoid consuming resources. A thread is an expensive operating system object and you can create a very limited number of them. A thread takes 5 operating system handles and a megabyte of virtual memory address space. No Dispose() method to release these handles early. A thread pool thread allows you to reuse a thread. It is important that you use a thread pool thread only when the work it does is limited, ideally not taking more than half a second. And not blocking often. It is therefore best suited for short bursts for work, not anything where performance matters. Handling I/O completion is an ideal task for a TP thread.

有可能通过使用线程以改善方案的性能。你会使用线程或任务&LT这样做;>使用TaskContinuationOptions.LongRunning。有一些硬性要求真正得到提高性能,它们是pretty僵硬:

It is possible to improve the performance of a program by using threads. You'd do so by using Thread or a Task<> that uses TaskContinuationOptions.LongRunning. There are some hard requirements to actually get a performance improvement, they are pretty stiff:


  • 您需要的更多的多个线程。在理想情况下,你可以找一份工作,当您使用两个线程完成了一半的时间。走近这个理想很难,更多的线程,你用它获取更难了。

  • 您需要一台机器有多个内核的处理器。轻松搞定这些天。你创建的线程数量应不超过可用内核的数量。使用更通常的的性能。

  • 您需要的作业类型这是计算密集型,具有处理器的执行引擎是有限的资源。这是相当普遍的,但肯定没有灌篮高手。许多工作是由I / O吞吐量实际限制,就像从一个文件或dBASE查询阅读。或通过在该处理器可以从RAM中读取数据的速率的限制。这样的工作不会从线程中获益,您将有多个执行引擎可用,但你仍然只有一个硬盘和一个内存总线。

  • 您需要一个算法,可以分布在多个线程的工作,而几乎没有任何需要同步。这通常以解决难题,许多算法在本质上是非常顺序,并且不容易并行化的。

  • 您将需要大量的时间和耐心,以获得$ ​​C $ C稳定,表现良好。编写线程code为硬盘和线程的崩溃,你的计划每月进行一次,或者偶尔会产生无效结果的比赛可能是一个重要的时间片。

  • You need more than one thread. In an ideal case, you can half the time needed to get a job done when you use two threads. Approaching that ideal is hard, the more threads you use the harder it gets.
  • You need a machine with a processor that has multiple cores. Easy to get these days. The number of threads you create should not exceed the number of available cores. Using more will usually lower performance.
  • You need the kind of job that's compute-bound, having the execution engine of the processor be the constrained resource. That's fairly common but certainly no slamdunk. Many jobs are actually limited by I/O throughput, like reading from a file or dbase query. Or are limited by the rate at which the processor can read data from RAM. Such jobs don't benefit from threads, you'll have multiple execution engines available but you still have only one disk and one memory bus.
  • You need an algorithm that can distribute the work across multiple threads without hardly any need for synchronization. That's usually the hard problem to solve, many algorithms are very sequential in nature and are not easily parallelizable.
  • You'll need a lot of time and patience to get the code stable and performing well. Writing threaded code is hard and a threading race that crashes your program once a month, or produces an invalid result occasionally can be a major time sink.

这篇关于主题/线程池或BackgroundWorker的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:28