Ejemplo n.º 1
0
 private void btnCancel_Click(object sender, EventArgs e)
 {
     Status          = UpdateStatus.Cancel;
     txtOutput.Text += string.Format("Canceling update...{0}", Environment.NewLine);
     thread.CancelAsync();
     revertFiles();
     Close();
 }
Ejemplo n.º 2
0
        void thread_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                long   fileLength = updateService.StartDownload(currentFile.Id);
                byte[] fileData   = new byte[0];

                while (fileData.Length < fileLength && !Status.Equals(UpdateStatus.Error) && !Status.Equals(UpdateStatus.Cancel))
                {
                    var buffer = updateService.Download(currentFile.Id, fileData.Length);

                    var concat = new byte[fileData.Length + buffer.Length];
                    Buffer.BlockCopy(fileData, 0, concat, 0, fileData.Length);
                    Buffer.BlockCopy(buffer, 0, concat, fileData.Length, buffer.Length);

                    fileData = new byte[concat.Length];
                    concat.CopyTo(fileData, 0);

                    double t1   = (double)fileData.Length / fileLength;
                    int    done = Convert.ToInt32(t1 * 100);
                    thread.ReportProgress(done);
                }

                //read old file into memory incase there is a error, delete it
                string path = Path.GetDirectoryName(Application.ExecutablePath) + currentFile.InstallPath;
                path = Path.Combine(path, currentFile.FileName);

                try
                {
                    var oldFileStream = File.Open(path, FileMode.Open, FileAccess.Read);
                    var oldFileData   = new byte[oldFileStream.Length];
                    oldFileStream.Read(oldFileData, 0, (int)oldFileStream.Length);
                    oldFileStream.Close();
                    File.Delete(path);

                    OldFiles.Add(new BackupFile {
                        FileData = oldFileData, FilePath = path
                    });
                }
                catch (Exception)
                {
                }

                //write new file to hd
                var fs = new FileStream(path, FileMode.CreateNew, FileAccess.Write);
                fs.Write(fileData, 0, fileData.Length);
                fs.Close();
                FileIds.Remove(currentFile.Id);
            }
            catch (Exception ex)
            {
                //write all old files back to hd and error out
                thread.ReportProgress(0);
                thread.CancelAsync();
                Status = UpdateStatus.Error;
            }
        }