public Form1(WorkerMeta meta) { InitializeComponent(); this.myMeta = meta; this.Icon = Properties.Resources.haru_sd_WM1UAm_256px; this.myBWorker = new BackgroundWorker(); this.myBWorker.WorkerSupportsCancellation = true; this.myBWorker.WorkerReportsProgress = true; this.myBWorker.DoWork += MyBWorker_DoWork; this.myBWorker.RunWorkerCompleted += MyBWorker_RunWorkerCompleted; this.myBWorker.ProgressChanged += MyBWorker_ProgressChanged; }
private void MyBWorker_DoWork(object sender, DoWorkEventArgs e) { WorkerMeta myMeta = e.Argument as WorkerMeta; if (File.Exists(myMeta.Patch)) { this.myBWorker.ReportProgress(3); this.CloseProcess(myMeta.Destination); File.Delete(myMeta.Destination); File.Move(myMeta.Patch, myMeta.Destination); e.Result = myMeta; } }
private void MyBWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) { if (e.Error is IOException) { MessageBox.Show(this, e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (e.Error is Exception) { MessageBox.Show(this, e.Error.Message + "\n" + e.Error.StackTrace, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } Environment.Exit(1); } else { if (e.Result != null) { WorkerMeta myMeta = e.Result as WorkerMeta; try { File.Delete(myMeta.Patch); } catch { } using (Process myProcess = new Process()) { myProcess.StartInfo.FileName = myMeta.Destination; if (My.Computer.OSFullName.IndexOf("Windows XP") == -1) { myProcess.StartInfo.Verb = "runas"; } myProcess.Start(); } Environment.Exit(0); } else { MessageBox.Show(this, "The updates somehow failed. Unknown Error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); Environment.Exit(1); } } }