From 4233d5e904e13b24c3170071c2aad49333bd4d02 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Thu, 20 Jul 2023 00:56:32 +0100 Subject: [PATCH] Fix threading stuff (?) --- Bloxstrap/App.xaml.cs | 2 ++ Bloxstrap/UI/NotifyIconWrapper.cs | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/Bloxstrap/App.xaml.cs b/Bloxstrap/App.xaml.cs index 17b9d50..e35be17 100644 --- a/Bloxstrap/App.xaml.cs +++ b/Bloxstrap/App.xaml.cs @@ -314,6 +314,8 @@ namespace Bloxstrap FinalizeExceptionHandling(exception); }); + NotifyIcon?.InitializeContextMenu(); + dialog?.ShowBootstrapper(); bootstrapperTask.Wait(); diff --git a/Bloxstrap/UI/NotifyIconWrapper.cs b/Bloxstrap/UI/NotifyIconWrapper.cs index f984648..c2f6549 100644 --- a/Bloxstrap/UI/NotifyIconWrapper.cs +++ b/Bloxstrap/UI/NotifyIconWrapper.cs @@ -12,7 +12,7 @@ namespace Bloxstrap.UI bool _disposed = false; private readonly System.Windows.Forms.NotifyIcon _notifyIcon; - private readonly MenuContainer _menuContainer = new(); + private MenuContainer? _menuContainer; private RobloxActivity? _activityWatcher; private ServerInformation? _serverInformationWindow; @@ -33,11 +33,16 @@ namespace Bloxstrap.UI }; _notifyIcon.MouseClick += MouseClickEventHandler; + } + public void InitializeContextMenu() + { + if (_menuContainer is not null) + return; + + _menuContainer = new(); _menuContainer.Dispatcher.BeginInvoke(_menuContainer.ShowDialog); - _menuContainer.ServerDetailsMenuItem.Click += (_, _) => ShowServerInformationWindow(); - _menuContainer.Closing += (_, _) => App.Logger.WriteLine("[NotifyIconWrapper::NotifyIconWrapper] Context menu container closed"); } @@ -53,7 +58,8 @@ namespace Bloxstrap.UI public async void OnGameJoin() { - _menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Visible); + if (_menuContainer is not null) + _menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Visible); if (App.Settings.Prop.ShowServerDetails) { @@ -64,7 +70,8 @@ namespace Bloxstrap.UI public void OnGameLeave(object? sender, EventArgs e) { - _menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Collapsed); + if (_menuContainer is not null) + _menuContainer.Dispatcher.Invoke(() => _menuContainer.ServerDetailsMenuItem.Visibility = Visibility.Collapsed); if (_serverInformationWindow is not null && _serverInformationWindow.IsVisible) _serverInformationWindow.Dispatcher.Invoke(_serverInformationWindow.Close); @@ -73,7 +80,7 @@ namespace Bloxstrap.UI public void MouseClickEventHandler(object? sender, System.Windows.Forms.MouseEventArgs e) { - if (e.Button != System.Windows.Forms.MouseButtons.Right) + if (e.Button != System.Windows.Forms.MouseButtons.Right || _menuContainer is null) return; _menuContainer.Activate(); @@ -137,7 +144,9 @@ namespace Bloxstrap.UI App.Logger.WriteLine($"[NotifyIconWrapper::Dispose] Disposing NotifyIcon"); - _menuContainer.Dispatcher.Invoke(_menuContainer.Close); + if (_menuContainer is not null) + _menuContainer.Dispatcher.Invoke(_menuContainer.Close); + _notifyIcon.Dispose(); _disposed = true;