Skip to content

Commit

Permalink
Prevent more than 1 concurrent update check
Browse files Browse the repository at this point in the history
  • Loading branch information
BlythMeister committed Sep 7, 2018
1 parent 016325a commit bc9c0e9
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 42 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ skip_commits:
- docs/*
- '**/*.html'
- '**/*.md'
version: '3.11.4.{build}'
version: '3.11.5.{build}'
skip_tags: true
image: Visual Studio 2017
clone_folder: c:\project\gallifrey
Expand Down
7 changes: 7 additions & 0 deletions src/Gallifrey.UI.Modern/ChangeLog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,13 @@
<Other>Dependency updates</Other>
</Others>
</Version>
<Version Number="3.11.5.0" Name="">
<Features/>
<Bugs>
<Bug>Prevent more than 1 concurrent update check</Bug>
</Bugs>
<Others />
</Version>
<!--
Template for VNext if not present above
<Version Number="0.0.0.0" Name="Pre-Release">
Expand Down
97 changes: 56 additions & 41 deletions src/Gallifrey/Versions/VersionControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class VersionControl : IVersionControl

private DateTime lastUpdateCheck;
private int updateErrorCount;
private long updateCheckOngoing;

public bool IsAutomatedDeploy => ApplicationDeployment.IsNetworkDeployed;
public Version DeployedVersion => ApplicationDeployment.CurrentDeployment.CurrentVersion;
Expand Down Expand Up @@ -82,61 +84,74 @@ public Task<UpdateResult> CheckForUpdates(bool manualCheck = false)
return Task.Run(() => UpdateResult.TooSoon);
}

try
{
using (var client = new WebClient())
{
client.DownloadData("https://releases.gallifreyapp.co.uk");
}
}
catch
if (Interlocked.Exchange(ref updateCheckOngoing, Thread.CurrentThread.ManagedThreadId) != -1)
{
return Task.Run(() => UpdateResult.NoInternet);
return Task.Run(() => UpdateResult.TooSoon);
}

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

try
{

if (ApplicationDeployment.CurrentDeployment.CheckForUpdate(false))
try
{
return Task.Run(() => ApplicationDeployment.CurrentDeployment.Update()).ContinueWith(task =>
using (var client = new WebClient())
{
SetVersionName();
UpdateInstalled = true;
UpdateReinstallNeeded = false;
UpdateStateChange?.Invoke(this, null);
updateErrorCount = 0;
return UpdateResult.Updated;
});
client.DownloadData("https://releases.gallifreyapp.co.uk");
}
}
catch
{
return Task.Run(() => UpdateResult.NoInternet);
}

return Task.Run(() =>
UpdateCheckOccured?.Invoke(this, manualCheck);
lastUpdateCheck = DateTime.UtcNow;

try
{
//If manual put a delay in here...the UI goes all weird if it's not
if (manualCheck)

if (ApplicationDeployment.CurrentDeployment.CheckForUpdate(false))
{
UpdateReinstallNeeded = false;
updateErrorCount = 0;
Task.Delay(TimeSpan.FromSeconds(2));
return Task.Run(() => ApplicationDeployment.CurrentDeployment.Update()).ContinueWith(task =>
{
SetVersionName();
UpdateInstalled = true;
UpdateReinstallNeeded = false;
UpdateStateChange?.Invoke(this, null);
updateErrorCount = 0;
return UpdateResult.Updated;
});
}
return UpdateResult.NoUpdate;
});
}
catch (TrustNotGrantedException)
{
UpdateReinstallNeeded = true;
UpdateStateChange?.Invoke(this, null);
updateErrorCount = updateErrorCount + 1;
return Task.Run(() => UpdateResult.ReinstallNeeded);

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;
}
}
catch (Exception)
finally
{
UpdateStateChange?.Invoke(this, null);
updateErrorCount = updateErrorCount + 1;
throw;
Interlocked.Exchange(ref updateCheckOngoing, -1);
}
}

Expand Down

0 comments on commit bc9c0e9

Please sign in to comment.