63 lines
2.0 KiB
C#
63 lines
2.0 KiB
C#
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Katteker.Gui.Properties;
|
|
|
|
namespace Katteker.Gui
|
|
{
|
|
/// <summary>
|
|
/// The wrapper to add Squirrel-capability to older Programs.
|
|
/// </summary>
|
|
public static class Wrapper
|
|
{
|
|
private static UpdateManager _manager;
|
|
|
|
/// <summary>
|
|
/// Checks for Updates.
|
|
/// </summary>
|
|
/// <returns>Task</returns>
|
|
public static Task CheckForUpdateAsync() => CheckForUpdateAsync(false);
|
|
|
|
/// <summary>
|
|
/// Checks for Updates.
|
|
/// </summary>
|
|
/// <param name="isStartup">Is this Method called on Startup of the Program.</param>
|
|
/// <returns>Task</returns>
|
|
public static async Task CheckForUpdateAsync(bool isStartup)
|
|
{
|
|
if (_manager == null && !UpdateManager.TryCreate(out _manager))
|
|
{
|
|
if (!isStartup)
|
|
{
|
|
MessageBox.Show(Resources.SquirrelWrapper_CheckForUpdate,
|
|
Resources.SquirrelWrapper_CheckForUpdate_Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
var releases = (await _manager.CheckForUpdateAsync().ConfigureAwait(false)).ToArray();
|
|
if (releases.Any() || !isStartup)
|
|
{
|
|
var thread = new Thread(ThreadProcess);
|
|
thread.SetApartmentState(ApartmentState.STA);
|
|
thread.Start(releases.LastOrDefault());
|
|
thread.Join();
|
|
}
|
|
}
|
|
|
|
private static void ThreadProcess(object argument)
|
|
{
|
|
var entry = (ReleaseEntry) argument;
|
|
using (var window = new UpdateWindow(_manager, entry))
|
|
{
|
|
var dialogResult = window.ShowDialog();
|
|
if (dialogResult == DialogResult.OK)
|
|
{
|
|
_manager.RestartApp();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |