@@ -103,6 +103,11 @@ private void SetupApplication()
103
103
}
104
104
105
105
_cancellation = new CancellationTokenSource ( ) ;
106
+
107
+ _cancellation . Token . Register ( ( ) => {
108
+ _cancellation . Dispose ( ) ;
109
+ if ( _exitRequested ) { Environment . Exit ( 0 ) ; }
110
+ } ) ;
106
111
}
107
112
108
113
@@ -142,6 +147,8 @@ private void ConfigureServices(IServiceCollection service)
142
147
143
148
#region System Tray Icon
144
149
150
+ private bool _exitRequested = false ;
151
+
145
152
private void InitializeTrayIcon ( )
146
153
{
147
154
_trayMenu = new ContextMenu ( ) ;
@@ -152,7 +159,10 @@ private void InitializeTrayIcon()
152
159
153
160
_trayMenu . Items . Add ( "Reset" , null , OnResetClickedOnClick ) ;
154
161
_trayMenu . Items . Add ( "-" ) ;
155
- _trayMenu . Items . Add ( "Exit" , null , ( sender , args ) => Application . Exit ( ) ) ;
162
+ _trayMenu . Items . Add ( "Exit" , null , ( sender , args ) => {
163
+ _exitRequested = true ;
164
+ Application . Exit ( ) ;
165
+ } ) ;
156
166
157
167
_trayIcon = new NotifyIcon
158
168
{
@@ -176,6 +186,13 @@ private void InitializeTrayIcon()
176
186
lastActiveWin = GetForegroundWindow ( ) ;
177
187
} ;
178
188
_trayIcon . BalloonTipClosed += ( sender , args ) => _notificationOpen = false ;
189
+
190
+ this . FormClosing += ( sender , e ) =>
191
+ {
192
+ // Hide and dispose the icon
193
+ _trayIcon . Visible = false ;
194
+ _trayIcon . Dispose ( ) ;
195
+ } ;
179
196
}
180
197
181
198
private void OnAutoStartClick ( object sender , EventArgs e )
0 commit comments