UpdateThread.cs 2.0 KB

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