1
0

UpdateThread.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. int sleepTime = 60 * 1000;
  28. if (Constants.DEV)
  29. {
  30. sleepTime = 1;
  31. }
  32. System.Threading.Thread.Sleep(sleepTime);
  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. }
  59. catch (Exception ex)
  60. {
  61. LogUtil.WriteErrorLog(ex, "获取更新失败!");
  62. }
  63. }
  64. }
  65. }