Using .NET 4 Tasks instead of Thread.QueueUserWorkItem

I read a bunch of articles about the new TPLs in .NET 4. Most of them recommend using Tasks as a replacement for Thread.QueueUserWorkItem. But from what I understand, tasks are not threads. So what happens in the following scenario when I want to use the Producer / Consumer queue using the new BlockingCollection class in .NET 4:

  • The queue is initialized with a parameter (say 100) to indicate the number of work tasks. Task.Factory.StartNew () is called to create a set of tasks.

  • Then a new work item is added to the queue, the consumer takes this task and performs it.

Now, based on the foregoing, there is a limit to the number of tasks that you can perform simultaneously using Thread.QueueUserWorkItem, the CLR will use the thread pool with the default pool size.

Basically what I'm trying to do is find out if the "Tasks with BlockingCollection" uses the appropriate script when I want to create a Windows service that will query the database for jobs that are ready to run. If the task is ready for execution, the timer in the Windows service (my only producer) will add a new work item to the queue, where the work will be picked up and completed by the work task.

In this case, does it make sense to use the Producer / Consumer queue? What about the number of work tasks?

+3
source share
1

, / - , . . .NET4 - , , .NET4 .
:

  • , , 1 , Thread.QueueUserWorkItem.
  • , , .
  • .
  • , .
+2

Source: https://habr.com/ru/post/1750909/


All Articles