Reputation: 1522
When creating a new task using Task.Run()
is it required to mark the method as async
and using await
to resume program flow?
Or is it possible to just call Task.Run()
and forget about the new thread? I.e., Task.Run()
just returns and the new thread starts running independently.
Upvotes: 2
Views: 150
Reputation: 564891
Or is it possible to just call Task.Run() and forget about the new thread?
Yes, this is possible. There is no requirement to handle the resulting Task
or Task<T>
in C#. This means that you don't need to use async
or await
, as well.
That being said, "fire and forget" tasks are typically a sign of a poor design. At the very minimum, it's typically a good idea to wrap the Task
in something that will await
it to handle and log exceptions.
Upvotes: 3