C#: Avoid Thread.Start use Task.Run
For the last few years I've been creating Thread
's to handle workload that would case the UI thread to freeze like the following: var t = new Thread(() => DoSomething()); t.Start(); t.Join(); //<- Freezes UI even though MessagePumping continues...
But recently I read that the use of Task
's is the more commonly used option nowadays. As its deeply integrated into the .NET Framework this approach works even better and using less code: await Task.Run(() => DoSomething()); //<- Doesn't freeze UI
Also Task can return values more easily. For example: var success = false; await Task.Run(() => success = DoSomething());