Visual Studio Code Analysis generates the warning "Dispose objects before losing scope" (CA2000) on the monitor
variable in this method.
private void MonitorJob(IJob job, CancellationToken cancellationToken)
{
var monitor = new JobMonitor(job, _backend); // <- CA2000
try
{
var task = monitor.Run(cancellationToken);
_activeJobs[task] = monitor;
}
catch
{
monitor.Dispose();
throw;
}
}
I understand what CA2000 does, and I'm usually able to work out why my code violates the rule and make the appropriate changes.
In this case, however, I'm stumped - is this really a false positive, or am I missing something?
Using Visual Studio 2015 Enterprise Edition, targeting .NET 4.5, using C# 6.