Search code examples
c#file-transfermicrosoft-bits

How can I retrieve the state of my BITS Job?


i am using the Microsoft.ConfigurationManagement.Messaging package to use the BITS. So far I have written following piece of code:

BitsJob job = new BitsJob(BitsJobType.Download, displayName);
BitsJobState jobState;
job.AddFileToJob(new Uri(source), destination); // source & destination are method parameters
job.Resume();
do
{
    // Do something with the job as long as its transferring
} while (/*Get job state here*/);

Now I want to retrieve the state of the job in the do-while-statement. I can't find a property or a method of the BitsJob instance returning me the current state of the job. Can someone tell me about how to get the current job state?


Solution

  • Figured out the answer by myself. For all those who hang around with this question. The best way is to retrieve it using the BitsJobProgress event. Following code sample can show that

    Job.BitsJobProgress += (sender, e) => 
    {
        JobState = e.JobState;
    };
    

    The BitsJobEventArgs include the job state. That way its possible to retrieve it and save it in a variable.