It's hard to answer this question without a bit more information, but I think you're misunderstanding the use of Task.Run()
and asynchronous code. If it's unimportant work, and you don't care about the result or its completion, you might ask if you need to be running it here in the first place. Secondly, Task.Run()
is typically used in order to execute long-running code that might otherwise block the UI thread. If you're making a DB call or writing to the File System, it could make sense to put this logic in a Task. An example being
void ReallyImportantFunction()
{
try
{
//Do really important work
Task.Run(() => NotSoImportantWork("some data"));
//Do really important work
}
catch (Exception ex)
{
/ Handle the exception
}
}
void NotSoImportantWork(string data)
{
//Do something here with the data
}
However this exposes you to data races if you actually do ever use whatever you're computing, or if your computation ever has any side-effects or mutations. If what you're saying is true and you never do look at the result, I would probably ask why it's in the method to begin with. Why compute really unimportant things right in the middle of important ones if you're never going to use them? If you are using the result, or mutating something you need, it makes sense for your code to properly await the result.
async void ReallyImportantFunction()
{
try
{
//Do really important work
var myResult = await IOIntensiveWork("some data");
//Do really important work
}
catch (Exception ex)
{
// Handle the exception
}
}
Task<MyResult> IOIntensiveWork(string data)
{
//Do something here with the data
}
It's usually cleaner if you can avoid async void
methods too, though there's nothing necessarily wrong with it. Just remember to always check for exceptions.
Edit: Based on what you've written in the comments, I think it makes sense to wrap your NotSoImportantWork()
in a task. This is not so much because it is unimportant, but rather because you are apparently making a network call, and there are a variety of problems that can occur while communicating with a service outside your control.
So you could just Task.Run(() => NotSoImportantWork("..."));
from ReallyImportantFunction()
, but be mindful of where you're catching exceptions and where your code potentially exits if one should occur. I would probably prefer to launch the Task.Run()
there rather than in NotSoImportantWork()
, because you could potentially use Task.Run(...).ConfigureAwait(false);
to squeeze that last bit of performance out of your code (this largely depends on how your code is structured).
Depending on where you're launching your task from, you can use ...ConfigureAwait(false);
to say that you don't need to the task to return to the current context when the operation completes. This can sometimes give performance improvements. Try and use it whenever it makes sense. For more information, have a look at this: When correctly use Task.Run and when just async-await
Without more of a context to your code, it's hard to go into much more detail than this. Hope it helps!