Skip to content

Commit 9a87ef4

Browse files
committed
added automatic updating
1 parent c200829 commit 9a87ef4

File tree

5 files changed

+93
-15
lines changed

5 files changed

+93
-15
lines changed

src/MainWindow.Designer.cs

Lines changed: 20 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/MainWindow.cs

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Runtime.InteropServices;
1010
using System.Text;
1111
using System.Text.RegularExpressions;
12+
using Task = System.Threading.Tasks.Task;
1213

1314
namespace OpenTimerResolution
1415
{
@@ -126,14 +127,18 @@ public MainWindow()
126127
return;
127128
}
128129

130+
#if !DEBUG
129131
CheckForUpdates();
132+
#endif
130133
}
131134

132135
private static string GetRequest(string uri, bool jsonaccept)
133136
{
134-
HttpClient client = new();
135-
client.BaseAddress = new(uri);
136-
client.Timeout = new(0, 0, 4);
137+
HttpClient client = new()
138+
{
139+
BaseAddress = new(uri),
140+
Timeout = new(0, 0, 4)
141+
};
137142

138143
if (jsonaccept)
139144
client.DefaultRequestHeaders
@@ -175,18 +180,52 @@ private void CheckForUpdates()
175180

176181
if (ver != programVersion)
177182
{
178-
DialogResult res = MessageBox.Show("Found a new update! Would you like to be redirected to the GitHub page?", "OpenTimerResolution", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
183+
DialogResult res = MessageBox.Show("Found a new update! Would you like to install it?", "OpenTimerResolution", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
184+
179185
switch (res)
180186
{
181187
case DialogResult.Yes:
182-
Process.Start(new ProcessStartInfo("cmd", "/c start https://github.com/TorniX0/OpenTimerResolution/releases/latest") { CreateNoWindow = true });
188+
189+
DialogResult type = MessageBox.Show("Framework dependent version?", "OpenTimerResolution", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
190+
191+
string url = (type == DialogResult.Yes) ?
192+
$"https://github.com/TorniX0/OpenTimerResolution/releases/download/{obj["tag_name"]}/OpenTimerResolution.exe" :
193+
$"https://github.com/TorniX0/OpenTimerResolution/releases/download/{obj["tag_name"]}/OpenTimerResolution_s.exe";
194+
195+
UpdateProgram(new Uri(url));
183196
break;
184197
case DialogResult.No:
185198
break;
186199
}
187200
}
188201
}
189202

203+
private void UpdateProgram(Uri url)
204+
{
205+
FileInfo file = new(Application.ExecutablePath);
206+
var oldFile = file.DirectoryName + "\\" + file.Name.Replace(file.Extension, ".old");
207+
File.Move(file.FullName, oldFile);
208+
209+
var task = Task.Run(async () => await Utilities.DownloadAsync(url, file.FullName));
210+
task.ContinueWith(x =>
211+
{
212+
Process.Start(new ProcessStartInfo("cmd", $"/c taskkill /f /im {file.Name} && start {file.FullName} && takeown.exe /f \"{oldFile}\" && del \"{oldFile}\" /f /q") { CreateNoWindow = true, Verb = "runas" });
213+
});
214+
215+
updateText.Visible = true;
216+
timerResolutionBox.ReadOnly = true;
217+
timerResolutionBox.Enabled = false;
218+
startButton.Enabled = false;
219+
purgeCacheButton.Enabled = false;
220+
updateConfigButton.Enabled = false;
221+
darkModeBox.Enabled = false;
222+
automaticCacheCleanBox.Enabled = false;
223+
intervalComboBox.Enabled = false;
224+
installScheduleButton.Enabled = false;
225+
logButton.Enabled = false;
226+
stopButton.Enabled = false;
227+
}
228+
190229
private void timerResolutionBox_TextChanged(object sender, EventArgs e)
191230
{
192231
if (timerResolutionBox.Text != string.Empty && timerResolutionBox.Text.Last() != '.')
@@ -476,6 +515,6 @@ private void updateConfigButton_Click(object sender, EventArgs e)
476515
MessageBox.Show("Config was updated successfully!", "OpenTimerResolution", MessageBoxButtons.OK, MessageBoxIcon.Information);
477516
}
478517

479-
#endregion
518+
#endregion
480519
}
481520
}

src/MainWindow.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3779,4 +3779,4 @@
37793779
QhUjAiAIVcz/B0PaSZM/Yc4lAAAAAElFTkSuQmCC
37803780
</value>
37813781
</data>
3782-
</root>
3782+
</root>

src/OpenTimerResolution.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
<StartupObject>OpenTimerResolution.Program</StartupObject>
99
<ApplicationIcon>program.ico</ApplicationIcon>
1010
<ApplicationManifest>app.manifest</ApplicationManifest>
11-
<AssemblyVersion>1.0.4.4</AssemblyVersion>
12-
<FileVersion>1.0.4.4</FileVersion>
13-
<Version>1.0.4.4</Version>
11+
<AssemblyVersion>1.0.4.5</AssemblyVersion>
12+
<FileVersion>1.0.4.5</FileVersion>
13+
<Version>1.0.4.5</Version>
1414
<DebugType>embedded</DebugType>
1515
<Product>OpenTimerResolution</Product>
1616
<Description>OpenTimerResolution is a lightweight open-source application that changes the resolution of the Windows system Timer to a specified value and has a memory cache cleaner included.</Description>

src/Utilities.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace OpenTimerResolution
8+
{
9+
public static class Utilities
10+
{
11+
#region Methods
12+
13+
public static async Task DownloadAsync(Uri requestUri, string filename)
14+
{
15+
HttpClient client = new();
16+
17+
using var s = await client.GetStreamAsync(requestUri);
18+
using var fs = new FileStream(filename, FileMode.Create);
19+
await s.CopyToAsync(fs);
20+
}
21+
22+
#endregion
23+
}
24+
}

0 commit comments

Comments
 (0)