123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- using System;
- using System.ComponentModel;
- using System.Threading;
- namespace Microsoft.Samples.AppUpdater
- {
- public class ServerPoller
- {
- private AppUpdater AppMan;
- private Thread PollerThread;
- private int _InitialPollDelay = 15;
- private int _PollInterval = 30;
- private bool _AutoStart = true;
- private bool _DownloadOnDetection = true;
- [DefaultValue(15)]
- [Description("Seconds between the first check for new updates.")]
- public int InitialPollInterval
- {
- get
- {
- return _InitialPollDelay;
- }
- set
- {
- _InitialPollDelay = value;
- }
- }
- [Description("Seconds between each subsequent check for updates.")]
- [DefaultValue(30)]
- public int PollInterval
- {
- get
- {
- return _PollInterval;
- }
- set
- {
- _PollInterval = value;
- }
- }
- [Description("Whether or not to automatically start the poll for for updates on startup.")]
- [DefaultValue(true)]
- public bool AutoStart
- {
- get
- {
- return _AutoStart;
- }
- set
- {
- _AutoStart = value;
- }
- }
- [DefaultValue(true)]
- [Description("Whether or not to automatically start downloading the update when detected.")]
- public bool DownloadOnDetection
- {
- get
- {
- return _DownloadOnDetection;
- }
- set
- {
- _DownloadOnDetection = value;
- }
- }
- public ServerPoller(AppUpdater appMan)
- {
- AppMan = appMan;
- }
- public void Start()
- {
- if (PollerThread == null)
- {
- PollerThread = new Thread(RunThread);
- }
- else if (!PollerThread.IsAlive)
- {
- PollerThread = new Thread(RunThread);
- }
- if (!PollerThread.IsAlive)
- {
- PollerThread.Start();
- }
- }
- public void Stop()
- {
- if (PollerThread != null && PollerThread.IsAlive)
- {
- PollerThread.Abort();
- PollerThread = null;
- }
- }
- public void RunThread()
- {
- int initialPollInterval = InitialPollInterval;
- try
- {
- Thread.Sleep(TimeSpan.FromSeconds(initialPollInterval));
- initialPollInterval = PollInterval;
- bool flag = false;
- try
- {
- flag = (AppMan.Manifest.State.Phase == UpdatePhases.Complete && AppMan.CheckForUpdates());
- }
- catch (Exception)
- {
- flag = false;
- }
- if (flag)
- {
- if (DownloadOnDetection)
- {
- AppMan.Downloader.Start();
- }
- Stop();
- }
- }
- catch (Exception)
- {
- }
- }
- }
- }
|