UpdateThread.cs 2.7 KB

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