Question

I want to include a Timer to call a timeout method on my IJob. Since Timer is Disposable, I made my IJob implement IDispose, but I'm not sure where to call Dispose() from, since IJob creation and destruction is handled by the scheduler.

Was it helpful?

Solution

I have one class that is responsible for all job creation, so I've implemented IJobListener on it, and registered it to listen for all jobs.

In JobWasExecuted, I've added the following toward the end of the method:

if (context.JobInstance.GetType().IsSubclassOf(typeof(MyBaseJobType)))
{
    (context.JobInstance as MyBaseJobType).Dispose();
}

I have no idea if this is the "correct" way, but it seems to be working. I do not think the scheduler Dispose()s automatically, because prior to adding the above, my timer seemed to keep going after the job had completed; apparently it was keeping the job thread alive. Also, IJob doesn't implement IDispose itself, so IMHO expected behavior would not be for the scheduler to Dispose.

EDIT: Another thought was to Dispose() of the timer in the timer callback, and also at the end of Execute(). Thinking this through, though: if the IJob throws a JobExecutionException, it won't be properly disposed; so this is probably not a good option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top