views:

167

answers:

4

I am using Quartz.NET in an application. What is the proper way to dispose of Quartz.NET.

Right now I am just doing

    if (_quartzScheduler != null)
    {
        _quartzScheduler = null;
    }

Is that enough or should I implement a dispose or something in the jobType class?

Seth

A: 

The docs don't say anything about IScheduler implementing IDisposable. If you have custom job types that grab and hold resources (file locks, database connections), you can implement IDispoable and override Dispose() on your object to release resources.

Dave Swersky
+1  A: 

This is not a complete example but might get you on the right path. I would implement something like this:

class customSchedulerClass : IDisposable
{

    private Component component = new Component();
    private bool disposed = false;

    public void scheduleSomeStuff()
    {
        //This is where you would implement the Quartz.net stuff
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SupressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if(!this=disposed)
        {
            if(disposing)
            {
                component.dispose;
            }
        }
        disposed = true;
    }       
}

Then with this you can do cool stuff like using statements:

public static void Main()
{
    using (customSchedulerClass myScheduler = new customSchedulerClass())
    {
        c.scheduleSomeStuff();
    }
    console.WriteLine("Now that you're out of the using statement the resources have been disposed");
}

So basically by implementing you code while inheriting the functionality of IDisposable you can then us the using statement and when you're done it will cleanly dispose your resources and keep things nice and clean. (Disclaimer, again this is not a complete example, just to get you in the right direction).

Tim C
A: 

Generally we don't need to set an object to null in order to dispose it off. If an object contains unmanaged resources then it should implement IDisposable (and be called by all its clients).

You can refere this similar post.

Amby
+2  A: 
scheduler.Shutdown(true);
TskTsk