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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

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

LuaScripterX
Luau & Lua VM Reverser
VIP
Rep: 260
Join Date: Mar 2022
Posts: 20
Thanks: 68
1y ago · Nov 4, 2024 4:30 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?
LuaScripterX · Luau VM & Bytecode Engineering
The following users thanked LuaScripterX for this post:
AimbotMath
Trigonometry & Smoothing
MEMBER
Rep: 285
Join Date: Nov 2023
Posts: 27
Thanks: 73
1y ago · Nov 4, 2024 8:46 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.
AimbotMath · Angular Normalization & Smooth Angles
ParanormalDev
MelonLoader / BepInEx Dev
MEMBER
Rep: 230
Join Date: Sep 2023
Posts: 21
Thanks: 58
1y ago · Nov 5, 2024 9:46 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.
ParanormalDev | MelonLoader & ImGui Modding