-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
91 lines (79 loc) · 3.64 KB
/
Program.cs
File metadata and controls
91 lines (79 loc) · 3.64 KB
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
using System;
using System.IO;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Linq;
using Velopack;
using Velopack.Sources;
namespace WindowsSmartTaskbar
{
internal static class Program
{
static Mutex? mutex = null;
private static string AppDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "WindowsSmartTaskbar");
private static string LastUpdateCheckFile => Path.Combine(AppDataFolder, "last_update_check.txt");
[DllImport("user32.dll")] private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
public const int HWND_BROADCAST = 0xffff;
public static readonly int WM_SHOWME = RegisterWindowMessage("WM_SHOWME_SMARTTASKBAR");
[DllImport("user32.dll")] public static extern int RegisterWindowMessage(string message);
[STAThread]
static void Main(string[] args)
{
VelopackApp.Build().Run();
bool createdNew;
mutex = new Mutex(true, "WindowsSmartTaskbar_SingleInstance_App", out createdNew);
if (!createdNew)
{
SendMessage((IntPtr)HWND_BROADCAST, WM_SHOWME, 0, 0);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool autostart = false;
foreach (var arg in args)
{
if (arg.Trim('"', '\'').Equals("-autostart", StringComparison.OrdinalIgnoreCase)) autostart = true;
}
// Check for updates once per week
CheckForUpdatesAsync().ConfigureAwait(false);
Application.Run(new MainForm(autostart));
}
static async Task CheckForUpdatesAsync()
{
while (true)
{
try {
if (!Directory.Exists(AppDataFolder)) Directory.CreateDirectory(AppDataFolder);
bool shouldCheck = false;
if (File.Exists(LastUpdateCheckFile)) {
if (DateTime.TryParse(File.ReadAllText(LastUpdateCheckFile), out DateTime lastCheck)) {
if ((DateTime.Now - lastCheck).TotalDays >= 7) shouldCheck = true;
} else {
shouldCheck = true;
}
} else {
shouldCheck = true;
}
if (shouldCheck) {
var source = new GithubSource("https://github.com/nRn-World/WindowsSmartTaskbar", string.Empty, false);
var manager = new UpdateManager(source);
if (manager.IsInstalled) {
var newVersion = await manager.CheckForUpdatesAsync();
if (newVersion != null) {
await manager.DownloadUpdatesAsync(newVersion);
File.WriteAllText(LastUpdateCheckFile, DateTime.Now.ToString());
string[] args = Environment.GetCommandLineArgs().Skip(1).ToArray();
manager.ApplyUpdatesAndRestart(newVersion, args);
} else {
File.WriteAllText(LastUpdateCheckFile, DateTime.Now.ToString());
}
}
}
} catch { }
await Task.Delay(TimeSpan.FromHours(12));
}
}
}
}