1

For long runnings tasks(Asynchronous) in C# 4.0 windows Application.

The choices are:

  1. BackgroundWorker (Thread Pool)

  2. Thread

The above two are recommended for long running tasks(throughout application lifetime). Any other choices? In Additon,

  • BackgroundWorker - Uses ThreadPool Thread and for long running process with UI updates.

  • Thread - User defined Thread (Thread Creation overhead etc.) and for long running process (can use priority)

    Which would be a best choice

    1. if no need for UI update,priority and for Thread (which is created only on application/process start)?

    2. if number of tasks increased? (Care of ThreadPool Starvation)

Any other interesting things?

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
C-va
  • 2,910
  • 4
  • 27
  • 42

2 Answers2

1

For long runnings tasks(Asynchronous) in C# 4.0 windows Application.

You would probably use a Task (TPL) with the LongRunning option. The TPL also runs on top of the ThreadPool but the option can make it reserve a thread.

if number of tasks increased? (Care of ThreadPool Starvation)

The ThreadPool will try to dynamically adapt, and you have the option to help it a little.

H H
  • 263,252
  • 30
  • 330
  • 514
1

BackgroundWorker also uses Threads but just abstracts some Thread creation and monitoring details.

So by using BackgroundWorker you avoid; - Having to deal with details - High cost of creation of new thread (+performance)

ThreadPool starvation will only happen if you require to run another async task before others end. There is not so much you can do here, because CPU is already running with full capacity.

I wouldn't go using Thread's directly. If you are not happy with the ThreadPool provided, maybe you can try implementing one for yourself(better not to do, just joking).

Mert Akcakaya
  • 3,109
  • 2
  • 31
  • 42
  • I wouldn't go using Thread's directly. Any other reason? – C-va Nov 07 '12 at 07:54
  • As I said, harder to handle, high cost of creation and ThreadPool uses the same Thread objects. ThreadPool already knows how many threads should utilize the CPU best. So you got a good system ready to use there. Why bother? – Mert Akcakaya Nov 07 '12 at 08:34