-
Notifications
You must be signed in to change notification settings - Fork 5
/
Program.cs
119 lines (101 loc) · 3.7 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Windows.Forms;
using BatteryNotifier.Forms;
using System.Threading.Tasks;
using Squirrel;
using BatteryNotifier.Helpers;
using System.Threading;
namespace BatteryNotifier
{
internal static class Program
{
public static UpdateManager? UpdateManager;
private static Form? MainForm;
private static string? version = UtilityHelper.AssemblyVersion;
private static readonly string EventName = "BatteryNotifier.Instance.WaitHandle";
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledExpection);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (EventWaitHandle eventWaitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, EventName, out bool createdNew))
{
if (createdNew)
{
MainForm = new Dashboard();
var dashboard = MainForm as Dashboard;
#if RELEASE
if (InternetConnectivityHelper.CheckForInternetConnection())
{
dashboard?.Notify("🤿 Checking for update ...");
Task.Run( () =>InitUpdateManager()).Wait();
Task UpdateTask = new(CheckForUpdates);
UpdateTask.Start();
version = UpdateManager?.CurrentlyInstalledVersion().ToString();
}
#endif
dashboard?.SetVersion(version);
Application.Run(dashboard);
}
else
{
(MainForm as Dashboard)?.Notify("Another instance of Battery Notifier is already running.");
}
}
}
catch (Exception e)
{
(MainForm as Dashboard)?.Notify(e.Message);
}
}
public static async Task InitUpdateManager()
{
try
{
UpdateManager = await UpdateManager.GitHubUpdateManager($@"{Constants.Constant.SourceRepositoryUrl}");
}
catch (Exception)
{
(MainForm as Dashboard)?.Notify("🕹 Could not initialize update manager!");
}
}
static async void CheckForUpdates()
{
try
{
var updateInfo = await UpdateManager?.CheckForUpdate()!;
if (updateInfo.ReleasesToApply.Count > 0)
{
var releaseEntry = await UpdateManager.UpdateApp();
if (releaseEntry != null)
{
(MainForm as Dashboard)?.Notify($"✅ Battery Notifier {releaseEntry.Version} downloaded. Restart to apply.");
}
}
else
{
(MainForm as Dashboard)?.Notify("✌ No Update Available");
}
}
catch (Exception)
{
(MainForm as Dashboard)?.Notify("💀 Could not update app!");
}
finally
{
Thread.Sleep(1000);
(MainForm as Dashboard)?.Notify(string.Empty);
}
}
static void OnUnhandledExpection(object? sender, UnhandledExceptionEventArgs args)
{
(MainForm as Dashboard)?.Notify("Unhandled exception occured.");
}
}
}