This project is read-only.

BitsJobs get disposed?

Nov 10, 2013 at 3:53 AM
Hi.

I have a really annoying problem: BitsJobs get disposed and therefore I'm getting NullReferenceExceptions when trying to access their properties. For example, in my event handler:
    private void OnBitsJobNotify(object sender, Bits.BitsJobNotifyEventArgs e)
    {
        if (base.InvokeRequired)
        {
            // e.Job is fine here 
            var method = new EventHandler<Bits.BitsJobNotifyEventArgs>(OnBitsJobNotify);
            base.Invoke(method, sender, e);
        }
        else
        {
            // e.Job.everything throws NullReferenceException
            // e.Job.disposed == true
        }
    }
I have a class which takes software name in constructor and contains methods for creating BITS jobs and getting only jobs created by that software (I'm using BitsJob.Decription to hold that information). This disposing thing is also happening in there:
    public List<BitsJob> GetJobs(JobType enumJobType)
    {
        Manager.EnumJobs();
        return Manager.Jobs.Values.Where(job => job.JobType == enumJobType && job.GetFromDescription("Software", "") == SoftwareName).ToList();
    }

    // Extension method
    public static T GetFromDescription<T>(this BitsJob bitsJob, string stringKey, T defaultValue)
    {
        // bitsJob.Description throws NullReference, bitsJob.disposed == true
    }
So, what is going on and how can I get this thing working?
Nov 15, 2013 at 4:19 PM
For the first one it sounds like you're not properly creating an instance of an object. SharpBits is a bit weird in that it gives you methods and properties that appear to be objects, but in reality there is nothing there. You need to create and assign for there to be an object to use.

Case in point: For the Second method GetJobs, this should work:
    public List<BitsJob> GetJobs(JobType enumJobType)
    {
        BitsJobs allBitsJobs = Manager.EnumJobs(JobOwner.CurrentUser);
        return allBitsJobs.Values.Where(job => job.JobType == enumJobType && job.GetFromDescription("Software", "") == SoftwareName).ToList();
    }

    // Extension method
    public static T GetFromDescription<T>(this BitsJob bitsJob, string stringKey, T defaultValue)
    {
        // bitsJob.Description throws NullReference, bitsJob.disposed == true
    }