Skip to content

Commit c710e00

Browse files
committed
Automatic Update
1 parent 9955b86 commit c710e00

File tree

8 files changed

+43
-42
lines changed

8 files changed

+43
-42
lines changed

DataCommander.Updater/DataCommander.Updater.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
<Reference Include="System.Xml" />
4747
</ItemGroup>
4848
<ItemGroup>
49-
<Compile Include="Updater.cs" />
5049
<Compile Include="UpdaterForm.cs">
5150
<SubType>Form</SubType>
5251
</Compile>

DataCommander.Updater/Program.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ static void Main(string[] args)
1212
try
1313
{
1414
Debugger.Launch();
15-
MessageBox.Show("Updating Data Commander...");
1615

1716
var applicationExeFileName = args[0];
18-
var updater = new Updater();
19-
updater.Update(applicationExeFileName);
20-
21-
MessageBox.Show("Data Commander updated and started.");
17+
var updaterDirectory = Environment.CurrentDirectory;
18+
Foundation.Deployment.Updater.Update(updaterDirectory, applicationExeFileName);
2219

2320
//Application.EnableVisualStyles();
2421
//Application.SetCompatibleTextRenderingDefault(false);

DataCommander.Updater/Updater.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

DataCommander/EventHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ private void Handle(DownloadingNewVersionStarted @event) => _updaterForm.Invoke(
1616
_updaterForm.Log("Downloading new version...");
1717
Application.DoEvents();
1818
});
19+
20+
private void Handle(DownloadProgressChanged @event) => _updaterForm.Invoke(() => _updaterForm.Log($"{@event.DownloadProgressChangedEventArgs.ProgressPercentage}% complete."));
1921
private void Handle(NewVersionDownloaded @event) => _updaterForm.Invoke(() => _updaterForm.Log("New version downloaded."));
2022

2123
private void Handle(CheckForUpdateCompleted @event) => _updaterForm.Invoke(() =>

DataCommander/Updater.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Net;
34
using System.Reflection;
45
using System.Threading.Tasks;
56
using Foundation;
@@ -19,6 +20,16 @@ public sealed class DownloadingNewVersionStarted : Event
1920
{
2021
}
2122

23+
public sealed class DownloadProgressChanged : Event
24+
{
25+
public readonly DownloadProgressChangedEventArgs DownloadProgressChangedEventArgs;
26+
27+
public DownloadProgressChanged(DownloadProgressChangedEventArgs downloadProgressChangedEventArgs)
28+
{
29+
DownloadProgressChangedEventArgs = downloadProgressChangedEventArgs;
30+
}
31+
}
32+
2233
public sealed class NewVersionDownloaded : Event
2334
{
2435
}
@@ -56,7 +67,8 @@ private async Task Handle(CheckForUpdates checkForUpdates)
5667
var guid = Guid.NewGuid();
5768
var updaterDirectory = Path.Combine(Path.GetTempPath(), guid.ToString());
5869
var zipFileName = Path.Combine(updaterDirectory, "Updater.zip");
59-
await DeploymentApplication.DownloadUpdater(address, updaterDirectory, zipFileName);
70+
await DeploymentApplication.DownloadUpdater(address, updaterDirectory, zipFileName,
71+
args => _eventPublisher(new DownloadProgressChanged(args)));
6072
_eventPublisher(new NewVersionDownloaded());
6173
DeploymentApplication.ExtractZip(zipFileName, updaterDirectory);
6274

Foundation.NetStandard-2.0/Deployment/Application.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,36 @@ public static async Task<Version> GetRemoteVersion(Uri address)
1818
return new Version(text);
1919
}
2020

21-
public static async Task DownloadUpdater(Uri address, string updaterDirectory, string zipFileName)
21+
public static async Task DownloadUpdater(Uri address, string updaterDirectory, string zipFileName,
22+
Action<DownloadProgressChangedEventArgs> eventHandler)
2223
{
2324
Directory.CreateDirectory(updaterDirectory);
2425

26+
var sequence = new Sequence();
27+
var previousEventTimestamp = 0;
28+
2529
using (var webClient = new WebClient())
30+
{
31+
webClient.DownloadProgressChanged += (sender, args) =>
32+
{
33+
if (sequence.Next() == 0)
34+
{
35+
previousEventTimestamp = UniversalTime.GetTickCount();
36+
eventHandler(args);
37+
}
38+
else
39+
{
40+
var current = UniversalTime.GetTickCount();
41+
var elapsed = current - previousEventTimestamp;
42+
if (elapsed >= 1000)
43+
{
44+
previousEventTimestamp = current;
45+
eventHandler(args);
46+
}
47+
}
48+
};
2649
await webClient.DownloadFileTaskAsync(address, zipFileName);
50+
}
2751
}
2852

2953
public static void ExtractZip(string zipFileName, string updaterDirectory)
@@ -34,10 +58,7 @@ public static void ExtractZip(string zipFileName, string updaterDirectory)
3458

3559
public static void StartUpdater(string updaterExeFileName)
3660
{
37-
var workingDirectory = Path.GetDirectoryName(updaterExeFileName);
38-
3961
var processStartInfo = new ProcessStartInfo();
40-
processStartInfo.WorkingDirectory = workingDirectory;
4162
processStartInfo.FileName = updaterExeFileName;
4263
processStartInfo.Arguments = $"{Quote(Environment.CurrentDirectory)}";
4364
Process.Start(processStartInfo);

Foundation.NetStandard-2.0/Deployment/Updater.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static void Update(string updaterDirectory, string applicationExeFileName
1616
Directory.Move(updaterDirectory, applicationDirectory);
1717
Directory.Delete(backupDirectory);
1818

19-
DeploymentCommandRepository.Save(ApplicationName, new DeleteUpdater());
19+
DeploymentCommandRepository.Save(ApplicationName, new DeleteUpdater {Directory = updaterDirectory});
2020

2121
var processStartInfo = new ProcessStartInfo();
2222
processStartInfo.WorkingDirectory = applicationDirectory;

Foundation.NetStandard-2.0/UniversalTime.cs

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33

44
namespace Foundation
55
{
6-
/// <summary>
7-
///
8-
/// </summary>
96
public sealed class UniversalTime : IDateTimeProvider
107
{
118
private static volatile int _sharedTickCount;
@@ -42,20 +39,10 @@ public UniversalTime(int increment, int adjustment)
4239
_incrementedDateTime = _sharedDateTime;
4340
}
4441

45-
/// <summary>
46-
///
47-
/// </summary>
4842
public static int TickCount => _sharedTickCount;
4943

50-
/// <summary>
51-
///
52-
/// </summary>
5344
public static UniversalTime Default { get; } = new UniversalTime(increment: 16, adjustment: 60 * 1000);
5445

55-
/// <summary>
56-
///
57-
/// </summary>
58-
/// <returns></returns>
5946
public static int GetTickCount()
6047
{
6148
_sharedTickCount = Environment.TickCount;
@@ -77,14 +64,10 @@ public DateTime UtcNow
7764
{
7865
var calculatedDateTime = _incrementedDateTime.AddMilliseconds(elapsed);
7966
if (_sharedDateTime < calculatedDateTime)
80-
{
8167
_sharedDateTime = calculatedDateTime;
82-
}
8368
}
8469
else
85-
{
8670
_sharedDateTime = DateTime.UtcNow;
87-
}
8871

8972
_incrementedTickCount = _sharedTickCount;
9073
_incrementedDateTime = _sharedDateTime;

0 commit comments

Comments
 (0)