using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Katteker.Properties;
namespace Katteker
{
///
/// The wrapper to add Katteker-capability to older Programs.
///
public static class UserInterface
{
private static UpdateManager _manager;
private static Action _restartApp;
///
/// Checks for Updates.
///
/// Task
public static Task CheckForUpdateAsync() => CheckForUpdateAsync(false);
///
/// Checks for Updates.
///
/// Is this Method called on Startup of the Program.
/// Task
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();
_restartApp = () => _manager.RestartApp();
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 = argument as ReleaseEntry;
using (var window = new UpdateWindow(_manager, entry))
{
var dialogResult = window.ShowDialog();
if (dialogResult == DialogResult.OK)
{
_restartApp?.Invoke();
}
}
}
}
}