BITS Upload : How to Retrieve the Job Status Using GUID or JobName

May 2, 2013 at 5:07 PM
Dear Team,

I'm successfully uploading file onto server using BITS , while sending each file onto server i'm able to check for the Job Status and perform my actions accordingly.

Please let me know how to get Job Status using GUID/Job Name? . I have to check using GUID because i'm storing it for polling purpose, so that I can check the Status later and Handle events accordingly?

Here is my code.
try
        {
            string[] files = null;
            BitsJobs MultipleJobs = null;


            string Path1 = @"C:\Upload1";
            if (Directory.Exists(Path1))
            {
                files = Directory.GetFiles(Path1);
            }


            if (files.Count() > 1)
            {


            }


            int x = files.Count();


            String[,] myArray3 = new string[x, x];

            Collection<BitsFileInfo> obj1 = new Collection<BitsFileInfo>();
            int i = 0;
            for (i = 0; i <= files.GetUpperBound(0)  i++)
            {

                DateTime DATE = System.DateTime.Now;

                JobType jtx = JobType.Upload;
                BitsManager BitsManager1 = new BitsManager();
                BitsJob bjobs;

                string JobName = DATE.ToString("ddMMyyyyhhmmssfff");

                bjobs = BitsManager1.CreateJob(JobName,  jtx);




                string Source = @"C:\RUN";
                string Destination = @"http:\\RUN201:12345/";

                BitsCredentials cred = new BitsCredentials();
                cred.AuthenticationScheme = AuthenticationScheme.Basic;
                cred.AuthenticationTarget = AuthenticationTarget.Server;
                cred.UserName = @"dc\admin";
                cred.Password = @"Hello@123#";
                bjobs.AddCredentials(cred);


                string name = files[i].ToString();
                string FileName = Path.GetFileName(name);

                string RemoteURL = Destination + @"/" + FileName;

                bjobs.AddFile(RemoteURL,  name);
                bjobs.Resume();

                JobState ds = 0;
                ds = bjobs.State;
                Guid ID = bjobs.JobId;
                // MultipleJobs.Add(ID, bjobs);
                if (!File.Exists(@"D:\log.txt"))
                {

                    File.CreateText(@"D:'log.txt");
                }


                using (StreamWriter w = File.AppendText(@"D:\log.txt"))
                {
                    Log(ID + "," + JobName, w);
                }
            }





            JobState bs = 0;
            //bs = bjobs.State;
            bool jobcomplete = false;
            // string Error = bjobs.Error; 

            //bjobs.NoProgressTimeout = 60;
            do
            {
                System.Threading.Thread.Sleep(19000);

                //  bs = bjobs.State;
                //Guid ID = bjobs.JobId;

                //MulipleJobs.Add(ID, bjobs);



                if (bs == JobState.Transferred)
                    jobcomplete = true;
                if (bs == JobState.Acknowledged)
                    jobcomplete = true;
                if (bs == JobState.Error)
                    jobcomplete = true;
                if (bs == JobState.Canceled)
                    jobcomplete = true;
            } while (!jobcomplete);



            int err = 0;
            string errdesc = null;

            BitsError ie = null;




            int errvalue;
            string contexterr = null;
            string errortext = null;


            if (bs == JobState.Error)
            {
                try
                {

                    //bs.GetError(out ie);
                    err = ie.ErrorCode;
                    errdesc = ie.Description;
                    //          ie.GetError(out cerr, out err );
                    //          errvalue = err;
                    //        //  contexterr = cerr;
                    //          ie.GetErrorDescription(
                    //    (uint)System.Threading.Thread.CurrentThread.CurrentCulture.LCID,
                    //    out errdesc);
                    //          if (errdesc != null)
                    //              errortext = errdesc;
                }
                catch (Exception ex)
                {
                    //          Debug.WriteLine("Problem getting error description: " +
                    //                     ex.Message);
                    //          Debug.WriteLine(ex.StackTrace);
                }
                finally
                {
                    //   bjobs.Suspend();
                }
            }
            //  else
            //      Job.Complete();
            // 
        }
        catch (Exception Es)
{
        }
    }






    public static void Log(string logMessage, TextWriter Writer)
    {

        Writer.WriteLine(" {0} {1} :{2}",DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString(), logMessage);
    }





}
Please help me in solving this issue
May 3, 2013 at 7:59 AM
Hi

if you want to know the job status using the job id you can ask it to the BitsManager object like in the snippet below
BitsManager manager = new BitsManager();
BitsJob job = manager.CreateJob("Downloader", JobType.Download);
job.AddFile(fromFile, toURL);
// get job id
Guid jobId = job.JobId;

// retrieve the job from manager
BitsJob currentJob  = manager.Jobs[jobId];
JobState jobState = currentJob.State;
May 4, 2013 at 5:32 AM
Really, Thank you Giovanni. Thanks alot