Performance Difference Between ThreadPool.QueueUserWorkItem and Task.Factory.StartNew

I have a code base using ThreadPool.QueueUserWorkItemin several places. I thought it would be nice to switch from using ThreadPool.QueueUserWorkItemto using Task.Factory.StartNewwith TaskScheduler.Defaultas a scheduler.

After the update, I saw the execution time of the application to heaven. It is like an online transnational application receiving requests and usually responding between 40 ms and 500 ms, which is acceptable. After switching to Tasks, I see that many transactions take from 4000 ms to 38000 ms to respond, which is unacceptable.

The stream is quite complex. It includes a synchronous loop for incoming transactions, which actually performs simple checks and inserts into the database. After that, some parallel actions are started, and the main loop proceeds to the next incoming transaction. Concurrent actions consist mainly of recording and intensively checking db data.

All registration actions were initiated in ThreadPool using

ThreadPool.QueueUserWorkItem(/*logging action*/)

The quality control action was initiated using

Task.Factory.StartNew(/*qc action*/,
     TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness)

After making changes, Registration actions were switched to

Task.Factory.StartNew(/*logging action*/,
     CancellationToken.None,
     TaskCreationOptions.None,
     TaskScheduler.Default)

And the quality control action has been switched to

Task.Factory.StartNew(/*qc action*/,
     CancellationToken.None,
     TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness,
     TaskScheduler.Default)

Is it likely that the registration actions stop the quality control action when executed with Task.Factory.StartNew in ThreadPoolScheduler (TaskScheduler.Default), but when they are executed directly on ThreadPool with QueueUserWorkItem, are they not?

, TaskCreationOptions.PreferFairness , ?

+4

All Articles