Skip to content

Commit 47934d2

Browse files
IReboIRebo
authored andcommitted
update
1 parent bf2437f commit 47934d2

1 file changed

Lines changed: 53 additions & 23 deletions

File tree

Form1.cs

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Threading;
45
using System.Threading.Tasks;
56
using System.Windows.Forms;
67
using Windows.ApplicationModel;
78
using Windows.Management.Deployment;
89
using xtrance.Properties;
10+
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
911

1012
namespace xtrance
1113
{
@@ -36,11 +38,22 @@ public Form1()
3638
PackageManager packageManager = new PackageManager();
3739
Package currentPackage = packageManager.FindPackageForUser(string.Empty, Package.Current.Id.FullName);
3840

39-
PackageUpdateAvailabilityResult status = currentPackage.CheckUpdateAvailabilityAsync().GetAwaiter().GetResult();
40-
labelUpdate.Invoke((Action)(() => labelUpdate.Text = status.Availability.ToString()));
41-
if (status.Availability == PackageUpdateAvailability.Required || status.Availability == PackageUpdateAvailability.Available)
41+
int retries = 5;
42+
while (retries >= 0)
4243
{
43-
buttonUpdate.Invoke((Action)(() => buttonUpdate.Visible = true));
44+
45+
PackageUpdateAvailabilityResult status = currentPackage.CheckUpdateAvailabilityAsync().GetAwaiter().GetResult();
46+
labelUpdate.Invoke((Action)(() => labelUpdate.Text = status.Availability.ToString()));
47+
if (status.Availability == PackageUpdateAvailability.NoUpdates)
48+
{
49+
break;
50+
}
51+
if (status.Availability == PackageUpdateAvailability.Required || status.Availability == PackageUpdateAvailability.Available)
52+
{
53+
buttonUpdate.Invoke((Action)(() => buttonUpdate.Visible = true));
54+
break;
55+
}
56+
retries--;
4457
}
4558
}
4659
catch (Exception ex)
@@ -184,29 +197,46 @@ private string Sanitize(string input)
184197

185198
private void buttonUpdate_Click(object sender, EventArgs e)
186199
{
187-
PackageManager pm = new PackageManager();
188-
Package currentPackage = pm.FindPackageForUser(string.Empty, Package.Current.Id.FullName);
189-
190-
var installTask = pm.AddPackageByAppInstallerFileAsync(
191-
currentPackage.GetAppInstallerInfo().Uri,
192-
AddPackageByAppInstallerOptions.ForceTargetAppShutdown,
193-
pm.GetDefaultPackageVolume());
200+
buttonOK.Enabled = false;
201+
buttonCancel.Enabled = false;
194202

195-
installTask.Progress = (installResult, progress) => labelUpdate.BeginInvoke(() =>
203+
new Thread(() =>
196204
{
197-
labelUpdate.Text = $"Progress: {progress.percentage} {progress.state}";
198-
});
205+
try
206+
{
207+
PackageManager pm = new PackageManager();
208+
Package currentPackage = pm.FindPackageForUser(string.Empty, Package.Current.Id.FullName);
199209

200-
var res = installTask.GetAwaiter().GetResult();
210+
var installTask = pm.AddPackageByAppInstallerFileAsync(
211+
currentPackage.GetAppInstallerInfo().Uri,
212+
AddPackageByAppInstallerOptions.ForceTargetAppShutdown,
213+
pm.GetDefaultPackageVolume());
201214

202-
if (res.IsRegistered == true)
203-
{
204-
uint res2 = RelaunchHelper.RegisterApplicationRestart(null, RelaunchHelper.RestartFlags.NONE);
205-
labelUpdate.Text = "Please close the application to update";
206-
} else
207-
{
208-
labelUpdate.Text = $"Error {res.ErrorText}";
209-
}
215+
installTask.Progress = (installResult, progress) => labelUpdate.BeginInvoke(() =>
216+
{
217+
labelUpdate.Invoke((Action)(() => labelUpdate.Text = $"Progress: {progress.percentage} {progress.state}"));
218+
});
219+
220+
var res = installTask.GetAwaiter().GetResult();
221+
222+
uint res2 = RelaunchHelper.RegisterApplicationRestart(null, RelaunchHelper.RestartFlags.NONE);
223+
224+
225+
if (res.IsRegistered == true)
226+
{
227+
labelUpdate.Invoke((Action)(() => labelUpdate.Text = "Status ok. Application should automatically restart..."));
228+
}
229+
else
230+
{
231+
labelUpdate.Invoke((Action)(() => labelUpdate.Text = $"Error {res.ErrorText}"));
232+
}
233+
}
234+
catch (Exception ex)
235+
{
236+
labelUpdate.Invoke((Action)(() => labelUpdate.Text = $"Error {ex.Message}"));
237+
}
238+
})
239+
{ IsBackground = true }.Start();
210240

211241
}
212242
}

0 commit comments

Comments
 (0)