UpdateThread.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. try
  49. {
  50. if (jo["statisticUrl"] != null)
  51. {
  52. string statisticUrl = jo["statisticUrl"].ToString();
  53. if (!string.IsNullOrEmpty(statisticUrl))
  54. {
  55. //用户统计 只通过uuid统计用户数量 不收集任何信息
  56. statisticUrl += "?uuid=" + CommonCode.GetUniqueUUID();
  57. HttpUtil.Get(statisticUrl);
  58. }
  59. }
  60. } catch (Exception){}
  61. string onlineVersion = jo["version"].ToString();
  62. if (onlineVersion.CompareTo(nowVersion) > 0)
  63. {
  64. App.Current.Dispatcher.Invoke((Action)(() =>
  65. {
  66. //检测到版本更新
  67. UpdateWindow.Show(jo);
  68. }));
  69. }
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. LogUtil.WriteErrorLog(ex, "获取更新失败!");
  75. }
  76. }
  77. }
  78. }