UpdateThread.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using GeekDesk.Constant;
  2. using GeekDesk.Control.Windows;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using Newtonsoft.Json.Linq;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace GeekDesk.MyThread
  15. {
  16. public class UpdateThread
  17. {
  18. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  19. public static void Update()
  20. {
  21. System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(UpdateApp))
  22. {
  23. IsBackground = true
  24. };
  25. t.Start();
  26. }
  27. private static void UpdateApp()
  28. {
  29. try
  30. {
  31. //等待1分钟后再检查更新 有的网络连接过慢
  32. System.Threading.Thread.Sleep(60 * 1000);
  33. string updateUrl;
  34. string nowVersion = ConfigurationManager.AppSettings["Version"];
  35. switch (appConfig.UpdateType)
  36. {
  37. case UpdateType.GitHub:
  38. updateUrl = ConfigurationManager.AppSettings["GitHubUpdateUrl"];
  39. break;
  40. default:
  41. updateUrl = ConfigurationManager.AppSettings["GiteeUpdateUrl"];
  42. break;
  43. }
  44. string updateInfo = HttpUtil.Get(updateUrl);
  45. if (!StringUtil.IsEmpty(updateInfo))
  46. {
  47. JObject jo = JObject.Parse(updateInfo);
  48. string onlineVersion = jo["version"].ToString();
  49. if (onlineVersion.CompareTo(nowVersion) > 0)
  50. {
  51. App.Current.Dispatcher.Invoke((Action)(() =>
  52. {
  53. //检测到版本更新
  54. UpdateWindow.Show(jo);
  55. }));
  56. }
  57. }
  58. } catch (Exception ex)
  59. {
  60. LogUtil.WriteErrorLog(ex, "获取更新失败!");
  61. }
  62. }
  63. }
  64. }