From aa76d9167aca0228ea9728b8339be2276d16e1dc Mon Sep 17 00:00:00 2001 From: TheDuffman85 Date: Wed, 14 Jan 2015 21:52:27 +0100 Subject: [PATCH] Bug fix --- Synology Download Station Adapter/Adapter.cs | 85 +++++++++++-------- Synology Download Station Adapter/Program.cs | 2 +- .../frmDownloadStation.cs | 2 +- 3 files changed, 51 insertions(+), 38 deletions(-) diff --git a/Synology Download Station Adapter/Adapter.cs b/Synology Download Station Adapter/Adapter.cs index 1f61e27..6ff4dfe 100644 --- a/Synology Download Station Adapter/Adapter.cs +++ b/Synology Download Station Adapter/Adapter.cs @@ -39,7 +39,8 @@ public class Adapter private static HttpListener _httpListener; private static Dictionary _fileDownloads; - private static WebRequest _checkNewReleaseRequest; + //private static WebRequest _checkNewReleaseRequest; + private static System.Timers.Timer _checkNewReleaseTimer; #endregion @@ -68,6 +69,11 @@ public static void Start() _fileDownloads = new Dictionary(); + _checkNewReleaseTimer = new System.Timers.Timer(); + _checkNewReleaseTimer.Elapsed +=_checkNewReleaseTimer_Elapsed; + _checkNewReleaseTimer.Interval = 43200000; // Every 12 hours + _checkNewReleaseTimer.Start() ; + try { _httpListener.Start(); @@ -96,6 +102,11 @@ public static void Start() } + private static void _checkNewReleaseTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) + { + CheckUpdateAsync(); + } + /// /// Based on code created by bennyborn /// https://github.com/bennyborn/ClickNLoadDecrypt/ @@ -623,57 +634,59 @@ public static void OpenDownloadStation() Process.Start("http://" + Properties.Settings.Default.Address); } } - - public static void CheckUpdate() + + public static void CheckUpdateAsync() { - try - { - _checkNewReleaseRequest = WebRequest.Create(LASTEST_RELEASE_URL); - _checkNewReleaseRequest.BeginGetResponse(new AsyncCallback(CheckNewRelease_Response), null); - } - catch - { - // Throw no error here - } + new Task(() => { CheckUpdate(); }).Start(); } - private static void CheckNewRelease_Response(IAsyncResult result) + private static void CheckUpdate() { - WebResponse response = _checkNewReleaseRequest.EndGetResponse(result); + try + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(LASTEST_RELEASE_URL); + request.Referer = "http://SynologyDownloadStationAdapte.update"; - if (response != null && + WebResponse response = request.GetResponse(); + + if (response != null && response.ResponseUri.Segments != null && response.ResponseUri.Segments.Length > 0) - { - int gitVersion = 0; - int currentVersion = 0; - string gitVersionStr = response.ResponseUri.Segments.Last(); - string currentVersionStr = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; - - if (int.TryParse(gitVersionStr.Replace(".", ""), out gitVersion) && - int.TryParse(currentVersionStr.Replace(".", ""), out currentVersion)) { - if (gitVersion > Properties.Settings.Default.CheckedVersion && - gitVersion > currentVersion) - { - Properties.Settings.Default.CheckedVersion = gitVersion; - Properties.Settings.Default.Save(); + int gitVersion = 0; + int currentVersion = 0; + string gitVersionStr = response.ResponseUri.Segments.Last(); + string currentVersionStr = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion; - frmSettings.Instance.BeginInvoke(new MethodInvoker(delegate - { - DialogResult mbResult = MessageBox.Show(frmSettings.Instance, "There is a new version available. Do you want to download it now?", "Synology Download Station Adapter", MessageBoxButtons.YesNo); + if (int.TryParse(gitVersionStr.Replace(".", ""), out gitVersion) && + int.TryParse(currentVersionStr.Replace(".", ""), out currentVersion)) + { + if (gitVersion > Properties.Settings.Default.CheckedVersion && + gitVersion > currentVersion) + { + Properties.Settings.Default.CheckedVersion = gitVersion; + Properties.Settings.Default.Save(); - if (mbResult == DialogResult.Yes) + frmSettings.Instance.BeginInvoke(new MethodInvoker(delegate { - Process.Start(LASTEST_RELEASE_URL); - } + DialogResult mbResult = MessageBox.Show(frmSettings.Instance, "There is a new version available. Do you want to download it now?", "Synology Download Station Adapter", MessageBoxButtons.YesNo); + + if (mbResult == DialogResult.Yes) + { + Process.Start(LASTEST_RELEASE_URL); + } - })); + })); + } } } } + catch + { + // Throw no error here + } } - + #endregion } } diff --git a/Synology Download Station Adapter/Program.cs b/Synology Download Station Adapter/Program.cs index f5d3eae..d06fa3d 100644 --- a/Synology Download Station Adapter/Program.cs +++ b/Synology Download Station Adapter/Program.cs @@ -45,7 +45,7 @@ static void Main(string[] args) Application.ApplicationExit += OnApplicationExit; // Check for updates - Adapter.CheckUpdate(); + Adapter.CheckUpdateAsync(); // Start the listener Adapter.Start(); diff --git a/Synology Download Station Adapter/frmDownloadStation.cs b/Synology Download Station Adapter/frmDownloadStation.cs index 0569519..de9dcf9 100644 --- a/Synology Download Station Adapter/frmDownloadStation.cs +++ b/Synology Download Station Adapter/frmDownloadStation.cs @@ -44,7 +44,7 @@ public static frmDownloadStation Instance if (_instance == null || _instance.IsDisposed) { - + _instance = new frmDownloadStation(); } }