iSpyApplication.downloader.backgroundWorker1_DoWork C# (CSharp) Method

backgroundWorker1_DoWork() private method

private backgroundWorker1_DoWork ( object sender, DoWorkEventArgs e ) : void
sender object
e DoWorkEventArgs
return void
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            string sUrlToReadFileFrom = Url;
            var url = new Uri(sUrlToReadFileFrom);
            var request = WebRequest.Create(url);
            var response = request.GetResponse();
            // gets the size of the file in bytes

            Int64 iSize = response.ContentLength;

            // keeps track of the total bytes downloaded so we can update the progress bar
            int iRunningByteTotal = 0;

            // use the webclient object to download the file

            using (Stream streamRemote = response.GetResponseStream())
            {
                if (streamRemote != null)
                {
                    streamRemote.ReadTimeout = 8000;
                    // loop the stream and get the file into the byte buffer
                    var byteBuffer = new byte[iSize];
                    int iByteSize;
                    while ((iByteSize = streamRemote.Read(byteBuffer, iRunningByteTotal, byteBuffer.Length - iRunningByteTotal)) > 0 && !backgroundWorker1.CancellationPending)
                    {
                        iRunningByteTotal += iByteSize;

                        // calculate the progress out of a base "100"
                        var dIndex = (double) (iRunningByteTotal);
                        var dTotal = (double) byteBuffer.Length;
                        var dProgressPercentage = (dIndex/dTotal);
                        var iProgressPercentage = (int) (dProgressPercentage*100);

                        // update the progress bar
                        backgroundWorker1.ReportProgress(iProgressPercentage);
                        int total = iRunningByteTotal;
                        UISync.Execute(() => lblProgress.Text = total + " / " + iSize);
                    }
                    if (!backgroundWorker1.CancellationPending)
                    {
                        if (SaveLocation.EndsWith(".xml"))
                        {
                            var ms = new MemoryStream(byteBuffer);
                            var doc = new XmlDocument();
                            try
                            {
                                doc.Load(ms);
                                doc.Save(SaveLocation);
                                success = true;

                            }
                            catch (Exception ex)
                            {
                                success = false;
                                Logger.LogExceptionToFile(ex);
                                DialogResult = DialogResult.Cancel;
                                aborting = true;
                            }
                            ms.Dispose();
                        }
                    }
                    else
                    {
                        Logger.LogMessageToFile("Update cancelled");
                    }
                }
                else
                {
                    Logger.LogErrorToFile("Response stream from " + Url + " failed");
                }
            }
            response.Close();
        }