Form1.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace Update
  13. {
  14. public partial class Form1 : Form
  15. {
  16. private readonly static string mainAppPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "..");
  17. private readonly static string mainAppFile = Path.Combine(mainAppPath, "dajiaoyan.exe");
  18. public Form1()
  19. {
  20. InitializeComponent();
  21. Task.Factory.StartNew(async () =>
  22. {
  23. await Task.Delay(2000);
  24. await Services.Update.UpdateApplication(
  25. "http://static.yuansiwei.com/app/dajiaoyan/ocr/dajiaoyan.zip",
  26. mainAppPath,
  27. UpdateProgress,
  28. AfterUpdate);
  29. });
  30. }
  31. public void UpdateProgress(string step, int nowP, int maxP)
  32. {
  33. Task.Factory.StartNew(() =>
  34. {
  35. BeginInvoke(new Action(() =>
  36. {
  37. stepProgressLabel.Text = step;
  38. stepProgressBar.Minimum = 0;
  39. stepProgressBar.Maximum = maxP;
  40. stepProgressBar.Value = nowP;
  41. }));
  42. });
  43. }
  44. public void AfterUpdate(bool ok, string info)
  45. {
  46. BeginInvoke(new Action(() =>
  47. {
  48. if (ok)
  49. {
  50. MessageBox.Show(info, "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  51. try
  52. {
  53. Task.Delay(2000);
  54. Process.Start(mainAppFile);
  55. }
  56. catch
  57. {
  58. }
  59. }
  60. else
  61. {
  62. MessageBox.Show(info, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
  63. }
  64. Close();
  65. }));
  66. }
  67. }
  68. }