ServerPoller.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. using System;
  2. using System.ComponentModel;
  3. using System.Threading;
  4. namespace Microsoft.Samples.AppUpdater
  5. {
  6. public class ServerPoller
  7. {
  8. private AppUpdater AppMan;
  9. private Thread PollerThread;
  10. private int _InitialPollDelay = 15;
  11. private int _PollInterval = 30;
  12. private bool _AutoStart = true;
  13. private bool _DownloadOnDetection = true;
  14. [DefaultValue(15)]
  15. [Description("Seconds between the first check for new updates.")]
  16. public int InitialPollInterval
  17. {
  18. get
  19. {
  20. return _InitialPollDelay;
  21. }
  22. set
  23. {
  24. _InitialPollDelay = value;
  25. }
  26. }
  27. [Description("Seconds between each subsequent check for updates.")]
  28. [DefaultValue(30)]
  29. public int PollInterval
  30. {
  31. get
  32. {
  33. return _PollInterval;
  34. }
  35. set
  36. {
  37. _PollInterval = value;
  38. }
  39. }
  40. [Description("Whether or not to automatically start the poll for for updates on startup.")]
  41. [DefaultValue(true)]
  42. public bool AutoStart
  43. {
  44. get
  45. {
  46. return _AutoStart;
  47. }
  48. set
  49. {
  50. _AutoStart = value;
  51. }
  52. }
  53. [DefaultValue(true)]
  54. [Description("Whether or not to automatically start downloading the update when detected.")]
  55. public bool DownloadOnDetection
  56. {
  57. get
  58. {
  59. return _DownloadOnDetection;
  60. }
  61. set
  62. {
  63. _DownloadOnDetection = value;
  64. }
  65. }
  66. public ServerPoller(AppUpdater appMan)
  67. {
  68. AppMan = appMan;
  69. }
  70. public void Start()
  71. {
  72. if (PollerThread == null)
  73. {
  74. PollerThread = new Thread(RunThread);
  75. }
  76. else if (!PollerThread.IsAlive)
  77. {
  78. PollerThread = new Thread(RunThread);
  79. }
  80. if (!PollerThread.IsAlive)
  81. {
  82. PollerThread.Start();
  83. }
  84. }
  85. public void Stop()
  86. {
  87. if (PollerThread != null && PollerThread.IsAlive)
  88. {
  89. PollerThread.Abort();
  90. PollerThread = null;
  91. }
  92. }
  93. public void RunThread()
  94. {
  95. int initialPollInterval = InitialPollInterval;
  96. try
  97. {
  98. Thread.Sleep(TimeSpan.FromSeconds(initialPollInterval));
  99. initialPollInterval = PollInterval;
  100. bool flag = false;
  101. try
  102. {
  103. flag = (AppMan.Manifest.State.Phase == UpdatePhases.Complete && AppMan.CheckForUpdates());
  104. }
  105. catch (Exception)
  106. {
  107. flag = false;
  108. }
  109. if (flag)
  110. {
  111. if (DownloadOnDetection)
  112. {
  113. AppMan.Downloader.Start();
  114. }
  115. Stop();
  116. }
  117. }
  118. catch (Exception)
  119. {
  120. }
  121. }
  122. }
  123. }