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) { } } } }