This question already has an answer here:
I have Windows service that needs to monitor different things in the system (connect to some WCF services, send some data, etc.) every minute.
For that I chose to use Quartz.NET jobs which look as follows:
internal class MonitorLaunchJob : IJob
{
public void Execute(IJobExecutionContext context)
{
MonitorBase[] monitors = GetAllMonitors();
foreach (var monitor in monitors)
var task = monitor.Launch();
}
}
monitor.Launch
is the method with the following signature:
public abstract Task Launch()
The overrides of this method may use long-running operations like await TcpClient.ConnectAsync()
and stuff like that. Those methods track their activity by themselves using a lot of logging, so I don't need to keep track of the Task
returned by monitor.Start()
. I need to just fire and forget.
Now, my question is: what do I do with this task
that Launch
method returns? I can't think of a reason to store it anywhere since it's a fire-and-forget operation. But on the other hand, if a method returns an object, it is weird to just ignore it. Do I need to do any sort of clean-up when the task
is completed? Will I face any performance issues if I just leave the task be and never track its state?
As said here https://blogs.msdn.microsoft.com/pfxteam/2012/03/25/do-i-need-to-dispose-of-tasks/, the short answer is "No. Don't bother disposing of your tasks."
Also good info here: Do I need to dispose of a Task?
Task
object means that you will be unable to monitor the state of the task, including that you will be unable to observe any exceptions that occur in the task. These limitations may or may not be a problem for you. See the marked duplicate for a more comprehensive explanation of the implications of not cleaning up your tasks. - Peter Duniho