My Start() has some really slow serial code so I figure i’ll throw it into a task and await it.
await new Task(() => { c.Start(); });
This compiles however it appears it doesn’t run the task unless i call Start(). Now instead of the simple one liner I have 3 lines. Is there a way I can write the below in one line?
var t = new Task(() => { c.Start(); });
t.Start();
await t;
You’re looking for
await Task.Run(c.Start). If you’re on .NET 4.0 (rather than 4.5), you can useawait Task.Factory.StartNew(c.Start).Prefer
Task.Factory.StartNewto manually constructing/starting aTask, andTask.RuntoTask.Factory.StartNew. Each one uses a more optimal implementation, and is not simply shorthand.