Пример #1
0
        public Task <UpdateResult> CheckForUpdates(bool manualCheck = false)
        {
            if (!IsAutomatedDeploy)
            {
                return(Task.Factory.StartNew(() => UpdateResult.NotDeployable));
            }

            if (lastUpdateCheck >= DateTime.UtcNow.AddMinutes(-5) && !manualCheck)
            {
                return(Task.Factory.StartNew(() => UpdateResult.TooSoon));
            }

            if (manualCheck)
            {
                UpdateCheckOccured?.Invoke(this, true);
            }
            else
            {
                UpdateCheckOccured?.Invoke(this, false);
            }
            lastUpdateCheck = DateTime.UtcNow;

            try
            {
                var updateInfo = ApplicationDeployment.CurrentDeployment.CheckForDetailedUpdate(false);

                if (updateInfo.UpdateAvailable && updateInfo.AvailableVersion > ApplicationDeployment.CurrentDeployment.CurrentVersion)
                {
                    return(Task.Factory.StartNew(() => ApplicationDeployment.CurrentDeployment.Update()).ContinueWith(task =>
                    {
                        SetVersionName();
                        UpdateInstalled = true;
                        NewVersionPresent?.Invoke(this, null);
                        return UpdateResult.Updated;
                    }));
                }

                return(Task.Factory.StartNew(() =>
                {
                    if (manualCheck)
                    {
                        Task.Delay(1500);
                    }
                    return UpdateResult.NoUpdate;
                }));
            }
            catch (TrustNotGrantedException)
            {
                UpdateReinstallNeeded = true;
                NewVersionPresent?.Invoke(this, null);
                return(Task.Factory.StartNew(() => UpdateResult.ReinstallNeeded));
            }
            catch (Exception)
            {
                return(Task.Factory.StartNew(() => UpdateResult.Error));
            }
        }
Пример #2
0
        public Task <UpdateResult> CheckForUpdates(bool manualCheck = false)
        {
            if (!IsAutomatedDeploy)
            {
                return(Task.Run(() => UpdateResult.NotDeployable));
            }

            if (lastUpdateCheck >= DateTime.UtcNow.AddMinutes(-3) && !manualCheck)
            {
                return(Task.Run(() => UpdateResult.TooSoon));
            }

            if (Interlocked.Exchange(ref updateCheckOngoing, 1) != -1 && !manualCheck)
            {
                return(Task.Run(() => UpdateResult.TooSoon));
            }

            try
            {
                try
                {
                    using (var client = new WebClient())
                    {
                        client.DownloadData("https://gallifrey-releases.blyth.me.uk");
                    }
                }
                catch
                {
                    return(Task.Run(() => UpdateResult.NoInternet));
                }

                UpdateCheckOccured?.Invoke(this, manualCheck);
                lastUpdateCheck = DateTime.UtcNow;

                try
                {
                    if (ApplicationDeployment.CurrentDeployment.CheckForUpdate(false))
                    {
                        return(Task.Run(() => ApplicationDeployment.CurrentDeployment.Update()).ContinueWith(task =>
                        {
                            SetVersionName();
                            UpdateInstalled = true;
                            UpdateReinstallNeeded = false;
                            UpdateStateChange?.Invoke(this, null);
                            updateErrorCount = 0;
                            return UpdateResult.Updated;
                        }));
                    }

                    return(Task.Run(() =>
                    {
                        //If manual put a delay in here...the UI goes all weird if it's not
                        if (manualCheck)
                        {
                            UpdateReinstallNeeded = false;
                            updateErrorCount = 0;
                            Task.Delay(TimeSpan.FromSeconds(2));
                        }

                        return UpdateResult.NoUpdate;
                    }));
                }
                catch (TrustNotGrantedException)
                {
                    UpdateReinstallNeeded = true;
                    UpdateStateChange?.Invoke(this, null);
                    updateErrorCount = updateErrorCount + 1;
                    return(Task.Run(() => UpdateResult.ReinstallNeeded));
                }
                catch (Exception)
                {
                    UpdateStateChange?.Invoke(this, null);
                    updateErrorCount = updateErrorCount + 1;
                    throw;
                }
            }
            finally
            {
                Interlocked.Exchange(ref updateCheckOngoing, -1);
            }
        }