Skip to content
This repository has been archived by the owner on Dec 26, 2020. It is now read-only.

Commit

Permalink
Fix MainForm still opening after crash; QOL re: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceanswave committed Sep 13, 2020
1 parent 5c736ce commit f7e2e7a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
24 changes: 20 additions & 4 deletions Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class MainForm : Form
private readonly System.Timers.Timer _pulseSimConnectStatusTimer = new System.Timers.Timer(PulseInterval);
private readonly ILogger<MainForm> _logger;
private readonly ISimConnectMqttAdapter _adapter;
private bool _hasShown = false;
private Color? _nextSimConnectStatusColor = null;
private Color? _nextMqttStatusColor = null;

Expand Down Expand Up @@ -73,13 +74,24 @@ protected override void WndProc(ref Message m)

#region Form Event Handlers
protected override void OnShown(EventArgs e)
{
_logger.LogInformation("Main Form Shown.");
base.OnShown(e);
}

protected override void OnLoad(EventArgs e)
{
var handle = Handle;
// Fire and forget as to not block the UI thread.
Task.Run(() => _adapter.Start(handle)).Forget();
Task.Run(() => { Task.Delay(1000); _adapter.Start(handle); }).Forget();

_logger.LogInformation("Main Form Shown.");
base.OnShown(e);
base.OnLoad(e);
}

protected override void OnActivated(EventArgs e)
{
_logger.LogInformation("Main Form Activated.");
base.OnActivated(e);
}

protected override void OnFormClosing(FormClosingEventArgs e)
Expand All @@ -88,8 +100,12 @@ protected override void OnFormClosing(FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
_logger.LogInformation("Main Form Hidden.");
}
else
{
_logger.LogInformation("Main Form Closing.");
}
_logger.LogInformation("Main Form Closing.");
}
#endregion

Expand Down
9 changes: 7 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class Program : ApplicationContext
#region Tray Application Initialization
public Program(bool showWindowOnStartup = true)
{
MainForm = _serviceProvider.GetService<MainForm>();

// Initialize Tray Icon
_trayIcon = new NotifyIcon()
{
Expand All @@ -58,6 +56,11 @@ public Program(bool showWindowOnStartup = true)
_trayIcon.ContextMenuStrip.Items.Add("Exit", null, (sender, e) => Application.Exit());
_trayIcon.Click += (sender, e) =>
{
if (MainForm == null)
{
MainForm = _serviceProvider.GetService<MainForm>();
}

if (!MainForm.Visible)
{
MainForm.Show();
Expand All @@ -72,6 +75,7 @@ public Program(bool showWindowOnStartup = true)

if (showWindowOnStartup)
{
MainForm = _serviceProvider.GetService<MainForm>();
MainForm.Show();
MainForm.Activate();
}
Expand Down Expand Up @@ -166,6 +170,7 @@ private static void Launch(bool showWindowOnStartup = true)
}
}
#endif
_logger.LogInformation("FSMosquitoClient Stopped.");
}

private static void GlobalExceptionHandler(object sender, UnhandledExceptionEventArgs e)
Expand Down
1 change: 1 addition & 0 deletions Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void ConfigureServices(IServiceCollection services)
{
builder
.AddSerilog()
.SetMinimumLevel(LogLevel.Error)
.AddFile("Logs/FSMosquitoClient-{Date}.txt");
});

Expand Down
7 changes: 7 additions & 0 deletions appsettings.dev.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"fs_mosquito_serverurl": "wss://dev.fsmosquito.com/mqtt/ws"
}

0 comments on commit f7e2e7a

Please sign in to comment.