4 Commits

Author SHA1 Message Date
287c008117 Make sure icon gets removed 2021-12-20 14:01:48 -05:00
42301d5242 More startup/upgrade cleanup 2021-12-20 13:57:21 -05:00
3d10cc9635 Clean up startup 2021-12-20 13:52:34 -05:00
714bd0384a Add IPC listener and tweak update messages 2021-12-20 13:43:14 -05:00
2 changed files with 38 additions and 20 deletions

View File

@@ -3,6 +3,7 @@ using Common.IO;
using Common.Wpf.Extensions;
using Squirrel;
using System;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
@@ -17,6 +18,7 @@ namespace WorkIndicator
public static string UpdateUrl = "https://github.com/ckaczor/WorkIndicator";
private Dispatcher _dispatcher;
private InterprocessMessageListener _commandLineListener;
[STAThread]
public static void Main(string[] args)
@@ -34,30 +36,39 @@ namespace WorkIndicator
_dispatcher = Dispatcher.CurrentDispatcher;
// Initialize the tray icon
TrayIcon.Initialize();
Task.Factory.StartNew(UpdateApp).ContinueWith(task => StartUpdate(task.Result.Result));
}
private void StartUpdate(bool updateRequired)
{
if (updateRequired)
return;
Task.Factory.StartNew(() =>
{
// Create an isolation handle to see if we are already running
_isolationHandle = ApplicationIsolation.GetIsolationHandle();
// If there is another copy then pass it the command line and exit
if (_isolationHandle == null)
{
try
{
InterprocessMessageSender.SendMessage(Environment.CommandLine);
}
catch { }
Shutdown();
return;
}
// Initialize the command line listener
_commandLineListener = new InterprocessMessageListener(Assembly.GetEntryAssembly().GetName().Name);
_commandLineListener.MessageReceived += HandleCommandLine;
// Initialize the tray icon
TrayIcon.Initialize();
Task.Factory.StartNew(CheckUpdate).ContinueWith(task => StartApplication(task.Result.Result));
}
private void StartApplication(bool updateRequired)
{
if (updateRequired)
return;
Task.Factory.StartNew(() =>
{
// Set automatic start into the registry
Current.SetStartWithWindows(Settings.Default.StartWithWindows);
@@ -66,17 +77,24 @@ namespace WorkIndicator
});
}
private async Task<bool> UpdateApp()
private void HandleCommandLine(object sender, InterprocessMessageListener.InterprocessMessageEventArgs e)
{
}
private async Task<bool> CheckUpdate()
{
return await UpdateCheck.CheckUpdate(HandleUpdateStatus);
}
private void HandleUpdateStatus(UpdateCheck.UpdateStatus status, string message)
{
if (status == UpdateCheck.UpdateStatus.None)
message = WorkIndicator.Properties.Resources.Loading;
if (status != UpdateCheck.UpdateStatus.Installing)
return;
// _dispatcher.Invoke(() => TrayIcon.SetText(message));
_dispatcher.Invoke(() => TrayIcon.ShowUpdateMessage(message));
TrayIcon.Dispose();
}
protected override void OnExit(ExitEventArgs e)
@@ -88,7 +106,7 @@ namespace WorkIndicator
TrayIcon.Dispose();
// Get rid of the isolation handle
_isolationHandle.Dispose();
_isolationHandle?.Dispose();
base.OnExit(e);
}

View File

@@ -68,9 +68,9 @@ namespace WorkIndicator
_initialized = true;
}
public static void SetText(string text)
public static void ShowUpdateMessage(string text)
{
_trayIcon.Text = text;
_trayIcon.ShowBalloonTip(200, Resources.ApplicationName, text, ToolTipIcon.None);
}
static void HandleContextMenuStripOpening(object sender, System.ComponentModel.CancelEventArgs e)