Home / Forums / Difference between Task.Run and ThreadPool.QueueUserWorkItem in modern .NET? [Discussion #3]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

Difference between Task.Run and ThreadPool.QueueUserWorkItem in modern .NET? [Discussion #3]

KernelGamer
C++ / Driver Dev
MEMBER
Rep: 340
Join Date: Sep 2022
Posts: 6
Thanks: 18
4y ago · Aug 22, 2021 12:27 PM
#1
Is there any real performance difference between Task.Run and ThreadPool.QueueUserWorkItem in .NET 8, or does Task.Run just wrap it with a Task object?
KernelGamer · C++ / Driver Dev
Windows Internals, system threads, and high-frequency game l...
The following users thanked KernelGamer for this post:
Algorithmic
Math & Algorithms
MEMBER
Rep: 45
Join Date: Jan 2020
Posts: 6
Thanks: 55
4y ago · Aug 22, 2021 2:11 PM
#2
Task.Run() uses the ThreadPool internally under the hood! The major advantage of Task.Run is that it returns a Task/Task<T> that supports async/await, cancellation tokens, continuation chaining with ContinueWith, and proper exception propagation. QueueUserWorkItem is lower level and harder to capture exceptions from.
Algorithmic · Math & Algorithms
Linear algebra, pathfinding, binary spatial partitioning, an...
DirectX_Pro
DirectX 11/12 Engineer
MEMBER
Rep: 69
Join Date: Aug 2021
Posts: 6
Thanks: 93
4y ago · Aug 23, 2021 4:11 PM
#3
Unless you are building an ultra-low-overhead specialized scheduler where the 48-byte Task allocation is unacceptable, always use Task.Run for clean async code.
DirectX_Pro · DirectX 11/12 Engineer
Direct3D pipelines, compute shaders, and swap chain presenta...