You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MainWindow.xaml.cs 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using Program;
  16. using System.Diagnostics;
  17. using System.IO;
  18. using System.ComponentModel;
  19. namespace InstallerUpdater
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. BackgroundWorker asyncDownloader = new BackgroundWorker();
  27. public MainWindow()
  28. {
  29. InitializeComponent();
  30. //MessageBox.Show("这是旧版");
  31. //MessageBox.Show("下载器更新功能正常");
  32. asyncDownloader.DoWork += AsyncDownloader_DoWork;
  33. asyncDownloader.RunWorkerCompleted += AsyncDownloader_RunWorkerCompleted;
  34. if (MessageBoxResult.Yes == MessageBox.Show("发现下载器更新,是否更新下载器?", "下载器更新", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes))
  35. {
  36. if (asyncDownloader.IsBusy)
  37. {
  38. MessageBox.Show("更新器已在运行");
  39. Process.Start(System.IO.Path.Combine(Updater.Dir, "Installer.exe"));
  40. Application.Current.Shutdown();
  41. }
  42. else
  43. asyncDownloader.RunWorkerAsync();
  44. }
  45. else
  46. {
  47. Updater.TellDismiss();
  48. Process.Start(System.IO.Path.Combine(Updater.Dir, "Installer.exe"));
  49. Application.Current.Shutdown();
  50. }
  51. }
  52. private void AsyncDownloader_RunWorkerCompleted(object? sender, RunWorkerCompletedEventArgs e)
  53. {
  54. if (e.Result == null)
  55. {
  56. MessageBox.Show("更新失败,请汇报");
  57. }
  58. else if ((bool)e.Result)
  59. {
  60. MessageBox.Show("已成功更新下载器,即将启动下载器");
  61. }
  62. else
  63. {
  64. MessageBox.Show("更新失败,请汇报");
  65. }
  66. Process.Start(System.IO.Path.Combine(Updater.Dir, "Installer.exe"));
  67. Application.Current.Shutdown();
  68. }
  69. private void AsyncDownloader_DoWork(object? sender, DoWorkEventArgs e)
  70. {
  71. if (asyncDownloader.CancellationPending)
  72. {
  73. e.Cancel = true;
  74. return;
  75. }
  76. else
  77. {
  78. if (Updater.UpdateInstaller())
  79. e.Result = true;
  80. else
  81. e.Result = false;
  82. }
  83. }
  84. }
  85. }