Back to Devexpress

CRR0041 - Use Task.Run instead of Task.Factory.StartNew

coderushforroslyn-400357-static-code-analysis-analyzers-library-crr0041-use-task-run-instead-of-task-factory-start-new.md

latest971 B
Original Source

CRR0041 - Use Task.Run instead of Task.Factory.StartNew

  • Mar 30, 2025

This analyzer detects Task.Factory.StartNew thread execution statements that can be unsafe.

csharp
Task.Factory.StartNew(() => { DemoMethodSync(); });
vb
Task.Factory.StartNew(Sub() DemoMethodSync())

To fix the issue, use the Task.Run method instead of Task.Factory.StartNew.

csharp
Task.Run(() => { DemoMethodSync(); });
vb
Task.Run(Sub() DemoMethodSync())

For information on why you need to use Task.Run instead of Task.Factory.StartNew, see the following blog post: Task.Run vs Task.Factory.StartNew.

See Also

Suppress Analyzers