Skip to content
Snippets Groups Projects
Commit 29a6a0d3 authored by Niko Ehrenfeuchter's avatar Niko Ehrenfeuchter :keyboard:
Browse files

Fix terminating the tray app from constructor.

Refers to #2
parent 45c2879c
No related branches found
No related tags found
No related merge requests found
...@@ -14,6 +14,8 @@ namespace ATXTray ...@@ -14,6 +14,8 @@ namespace ATXTray
{ {
private static readonly string AppTitle = Path.GetFileNameWithoutExtension(Application.ExecutablePath); private static readonly string AppTitle = Path.GetFileNameWithoutExtension(Application.ExecutablePath);
private static readonly Timer AppTimer = new Timer(1000); private static readonly Timer AppTimer = new Timer(1000);
private static bool _terminate = false;
private static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory; private static readonly string BaseDir = AppDomain.CurrentDomain.BaseDirectory;
private static readonly string ConfigFile = Path.Combine(BaseDir, "configuration.xml"); private static readonly string ConfigFile = Path.Combine(BaseDir, "configuration.xml");
private static readonly string StatusFile = Path.Combine(BaseDir, "status.xml"); private static readonly string StatusFile = Path.Combine(BaseDir, "status.xml");
...@@ -55,15 +57,23 @@ namespace ATXTray ...@@ -55,15 +57,23 @@ namespace ATXTray
try { try {
_config = ServiceConfig.Deserialize(ConfigFile); _config = ServiceConfig.Deserialize(ConfigFile);
ReadStatus(); ReadStatus();
SetupContextMenu();
} }
catch (Exception ex) { catch (Exception ex) {
_notifyIcon.ShowBalloonTip(10000, AppTitle, var msg = "Error during initialization: " + ex.Message;
"Unable to read config / status: " + ex.Message, ToolTipIcon.Error); _notifyIcon.ShowBalloonTip(5000, AppTitle, msg, ToolTipIcon.Error);
System.Threading.Thread.Sleep(10000); // we cannot terminate the message loop (Application.Run()) while the constructor
_notifyIcon.Visible = false; // is being run as it is not active yet - therefore we simply remember that we want
Application.Exit(); // to exit and evaluate this in the "Elapsed" handler:
_terminate = true;
System.Threading.Thread.Sleep(5000);
} }
AppTimer.Elapsed += AppTimerElapsed;
AppTimer.Enabled = true;
}
private void SetupContextMenu() {
_miExit.Text = @"Exit"; _miExit.Text = @"Exit";
_miExit.Click += MiExitClick; _miExit.Click += MiExitClick;
...@@ -98,9 +108,11 @@ namespace ATXTray ...@@ -98,9 +108,11 @@ namespace ATXTray
}); });
_notifyIcon.ContextMenuStrip = _cmStrip; _notifyIcon.ContextMenuStrip = _cmStrip;
}
AppTimer.Elapsed += AppTimerElapsed;
AppTimer.Enabled = true; private void AutoTxTrayExit() {
_notifyIcon.Visible = false;
Application.Exit();
} }
/// <summary> /// <summary>
...@@ -115,6 +127,11 @@ namespace ATXTray ...@@ -115,6 +127,11 @@ namespace ATXTray
} }
private void AppTimerElapsed(object sender, ElapsedEventArgs e) { private void AppTimerElapsed(object sender, ElapsedEventArgs e) {
if (_terminate) {
AutoTxTrayExit();
return;
}
UpdateSvcRunning(); UpdateSvcRunning();
var heartBeat = "?"; var heartBeat = "?";
...@@ -142,8 +159,7 @@ namespace ATXTray ...@@ -142,8 +159,7 @@ namespace ATXTray
} }
private void MiExitClick(object sender, EventArgs e) { private void MiExitClick(object sender, EventArgs e) {
_notifyIcon.Visible = false; AutoTxTrayExit();
Application.Exit();
} }
private void ShowContextMenu(object sender, EventArgs e) { private void ShowContextMenu(object sender, EventArgs e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment