public MkPsf2Worker() { progressStruct = new ProgressStruct(); fileCount = 0; maxFiles = 0; WorkerReportsProgress = true; WorkerSupportsCancellation = true; }
public BackgroundWorkerProgressPopup(Window owner, BackgroundWorker backgroundWorker, ProgressStruct progressStruct) { Owner = owner; _backgroundWorker = backgroundWorker; _progressStruct = progressStruct; _progressStruct.PropertyChanged += ProgressStructOnPropertyChanged; InitializeComponent(); }
private void Window_Loaded(object sender, RoutedEventArgs e) { //this.Visibility = Visibility.Hidden; //---------------------------------------------------- //---------------------------------------------------- StatusTextBlock.Text = "Проверка наличия обновлений"; //---------------------------------------------------- //---------------------------------------------------- _updateBackgroundWorker.ProgressChanged += (s, args) => { CommonProgressLabel.Content = args.ProgressPercentage; UpdateProgressBar.Value = args.ProgressPercentage; var ps = (ProgressStruct)args.UserState; if (!ps.Equals(null)) { if (ps.StatusText.Trim() != string.Empty) { StatusTextBlock.Text = ps.StatusText; } if (ps.StatusText.Trim() != string.Empty) { FileProgressLabel.Content = Math.Round(ps.DownloadPerc, 2); DownloadProgressBar.Value = Convert.ToInt32(Math.Round(ps.DownloadPerc, 0)); } } }; _updateBackgroundWorker.DoWork += (s, args) => { _canRun = CheckCanRun(); switch (_connectionArg) { case "-lc": App.FtpUrl = @"ftp://192.168.1.200/fa2_update/"; break; case "-ic": App.FtpUrl = @"ftp://82.209.219.219:21/fa2_update/"; break; case "-mc": App.FtpUrl = @"ftp://192.168.1.200/fa2_update/"; break; } CheckUpdates(); var ps = new ProgressStruct { DownloadPerc = 0, StatusText = "Запуск программы" }; _updateBackgroundWorker.ReportProgress(100, ps); }; _updateBackgroundWorker.RunWorkerCompleted += (s, args) => { //---------------------------------------------------- //---------------------------------------------------- StatusTextBlock.Text = "Запуск программы"; //---------------------------------------------------- //---------------------------------------------------- _arguments = App.AppArguments; string startArg = "-w"; if (_arguments.Length != 0) { startArg = _arguments[0]; } if (!ModeParameters.Contains(startArg)) { MessageBox.Show("Исполняемый файл программы не найден!", "Запуск невозможен", MessageBoxButton.OK, MessageBoxImage.Error); Environment.Exit(0); } StatusTextBlock.Text = "Тестирование подключения..."; if (CheckCanRun()) { StatusTextBlock.Text = "OK"; StatusTextBlock.Text = "Старт программы..."; string mainFilePath = Path.Combine(_mainDirectory, "MainProgram"); var startInfo = new ProcessStartInfo { FileName = Path.Combine(mainFilePath, "FA2.exe"), Arguments = startArg + " " + _connectionArg, }; try { Process.Start(startInfo); StatusTextBlock.Text = "OK"; } catch (Exception exp) { MessageBox.Show("Неверный параметр запуска\n" + exp.Message, "Запуск невозможен", MessageBoxButton.OK, MessageBoxImage.Error); } } else { MessageBox.Show( "Ведуться техниеские работы.\nПопробуйте запустить приложение через некоторое время.", "Запуск невозможен", MessageBoxButton.OK, MessageBoxImage.Information); } Application.Current.Shutdown(); }; _updateBackgroundWorker.RunWorkerAsync(); }
private void UpdateProgram() { bool isUpdateOK = true; try { string updatesPath = Path.Combine(_mainDirectory, "update"); if (Directory.Exists(updatesPath)) { Directory.Delete(updatesPath, true); } Directory.CreateDirectory(updatesPath); List <FilesInfoStruct> l3 = GetFilesNamesForUpdate(); //---------------------------------------------------- //---------------------------------------------------- var ps = new ProgressStruct { DownloadPerc = 0, StatusText = "Загрузка обновлений" }; _updateBackgroundWorker.ReportProgress(5, ps); //---------------------------------------------------- //---------------------------------------------------- //Thread.Sleep(3000); decimal perc = 100 / l3.Count; decimal allperc = 0; foreach (var filesInfoStruct in l3) { using (var request = new WebClient()) { string filePath = Path.Combine(updatesPath, filesInfoStruct.Name); try { request.Credentials = new NetworkCredential(App.FtpUser, App.FtpPass); byte[] fileData = request.DownloadData(filesInfoStruct.FullName); using (FileStream file = File.Create(filePath)) { file.Write(fileData, 0, fileData.Length); file.Close(); } allperc = allperc + perc; //---------------------------------------------------- //---------------------------------------------------- var ps5 = new ProgressStruct { DownloadPerc = allperc, StatusText = "Загрузка обновлений" }; _updateBackgroundWorker.ReportProgress(15, ps5); //---------------------------------------------------- //---------------------------------------------------- //Thread.Sleep(500); } catch (Exception) { File.Delete(filePath); isUpdateOK = false; } } } if (isUpdateOK) { //---------------------------------------------------- //---------------------------------------------------- var ps2 = new ProgressStruct { DownloadPerc = 100, StatusText = "Завершение основного процесса программы" }; _updateBackgroundWorker.ReportProgress(30, ps2); //---------------------------------------------------- //---------------------------------------------------- //Thread.Sleep(3000); try { var currentUser = WindowsIdentity.GetCurrent().Name; // Start the child process. var p = new Process { StartInfo = { CreateNoWindow = false, UseShellExecute = false, FileName = "TASKKILL", WindowStyle = ProcessWindowStyle.Normal, Arguments = currentUser != null ? "/F /FI \"USERNAME eq " + currentUser + "\" /IM fa2.exe" : "/F /IM fa2.exe" } }; p.Start(); p.WaitForExit(); } catch (Exception msgEx) { MessageBox.Show(msgEx.Message); } //---------------------------------------------------- //---------------------------------------------------- var ps1 = new ProgressStruct() { DownloadPerc = 100, StatusText = "Удаление устаревших версий файлов программы" }; _updateBackgroundWorker.ReportProgress(45, ps1); //---------------------------------------------------- //---------------------------------------------------- //Thread.Sleep(3000); foreach (var filesInfoStruct in l3) { if (filesInfoStruct.Name == "report") { continue; } string destFilePath = Path.Combine(Path.Combine(_mainDirectory, "MainProgram"), filesInfoStruct.Name); try { File.Delete(destFilePath); } catch { } } //---------------------------------------------------- //---------------------------------------------------- var ps3 = new ProgressStruct { DownloadPerc = 100, StatusText = "Установка обновлений" }; _updateBackgroundWorker.ReportProgress(60, ps3); //---------------------------------------------------- //---------------------------------------------------- foreach (var filesInfoStruct in l3) { switch (filesInfoStruct.Name) { case "report": continue; case "ver": try { File.Copy(Path.Combine(updatesPath, filesInfoStruct.Name), Path.Combine(_mainDirectory, filesInfoStruct.Name), true); } catch { } continue; default: try { string sourceFilePath = Path.Combine(updatesPath, filesInfoStruct.Name); string destFilePath = Path.Combine(Path.Combine(_mainDirectory, "MainProgram"), filesInfoStruct.Name); File.Copy(sourceFilePath, destFilePath, true); } catch { } break; } } } else { MessageBox.Show("Ошибка обновления", "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } //---------------------------------------------------- //---------------------------------------------------- var ps4 = new ProgressStruct { DownloadPerc = 100, StatusText = "Удаление временных файлов" }; _updateBackgroundWorker.ReportProgress(95, ps4); if (Directory.Exists(updatesPath)) { Directory.Delete(updatesPath, true); } //---------------------------------------------------- //---------------------------------------------------- var ps6 = new ProgressStruct { DownloadPerc = 100, StatusText = "Удаление временных файлов" }; _updateBackgroundWorker.ReportProgress(100, ps6); //---------------------------------------------------- //---------------------------------------------------- //Thread.Sleep(500); } catch (Exception exp) { MessageBox.Show(exp.Message, "Ошибка обновления!", MessageBoxButton.OK, MessageBoxImage.Error); } }
public GenhCreatorWorker() { this.progressStruct = new VGMToolbox.util.ProgressStruct(); WorkerReportsProgress = true; WorkerSupportsCancellation = true; }