22{
33 using System ;
44 using System . Threading . Tasks ;
5+ using ElectronNET ;
56 using ElectronNET . API ;
67 using ElectronNET . Common ;
78 using ElectronNET . Runtime . Controllers ;
@@ -81,9 +82,22 @@ protected void HandleStopped()
8182 ( this . aspNetLifetimeAdapter . IsNullOrStopped ( ) ) )
8283 {
8384 this . TransitionState ( LifetimeState . Stopped ) ;
85+
86+ // Everything is fully stopped – fire the OnQuit callback.
87+ Task . Run ( this . RunQuitCallback ) ;
8488 }
8589 }
8690
91+ /// <summary>
92+ /// Invoked when ASP.NET lifetime enters Stopping (ApplicationStopping).
93+ /// We only trigger the OnWillQuit callback here; the actual state
94+ /// transition to Stopping is handled in <see cref="HandleStopped"/>.
95+ /// </summary>
96+ protected void HandleStopping ( )
97+ {
98+ Task . Run ( this . RunWillQuitCallback ) ;
99+ }
100+
87101 protected abstract override Task StopCore ( ) ;
88102
89103 private void SocketBridge_Ready ( object sender , EventArgs e )
@@ -108,10 +122,67 @@ private void AspNetLifetimeAdapter_Stopped(object sender, EventArgs e)
108122
109123 private void AspNetLifetimeAdapter_Stopping ( object sender , EventArgs e )
110124 {
125+ this . HandleStopping ( ) ;
126+ }
127+
128+ private async Task RunWillQuitCallback ( )
129+ {
130+ var events = ElectronNetRuntime . Options ? . Events ;
131+ var handler = events ? . OnWillQuit ;
132+
133+ if ( handler == null )
134+ {
135+ return ;
136+ }
137+
138+ try
139+ {
140+ await handler ( ) . ConfigureAwait ( false ) ;
141+ }
142+ catch ( Exception ex )
143+ {
144+ Console . WriteLine ( "Exception while executing OnWillQuit callback.\n " + ex ) ;
145+ // We are already stopping; no need to call this.Stop() here.
146+ }
147+ }
148+
149+ private async Task RunQuitCallback ( )
150+ {
151+ var events = ElectronNetRuntime . Options ? . Events ;
152+ var handler = events ? . OnQuit ;
153+
154+ if ( handler == null )
155+ {
156+ return ;
157+ }
158+
159+ try
160+ {
161+ await handler ( ) . ConfigureAwait ( false ) ;
162+ }
163+ catch ( Exception ex )
164+ {
165+ Console . WriteLine ( "Exception while executing OnQuit callback.\n " + ex ) ;
166+ }
111167 }
112168
113169 private async Task RunReadyCallback ( )
114170 {
171+ var events = ElectronNetRuntime . Options ? . Events ;
172+ if ( events ? . OnBeforeReady != null )
173+ {
174+ try
175+ {
176+ await events . OnBeforeReady ( ) . ConfigureAwait ( false ) ;
177+ }
178+ catch ( Exception ex )
179+ {
180+ Console . WriteLine ( "Exception while executing OnBeforeReady callback. Stopping...\n " + ex ) ;
181+ this . Stop ( ) ;
182+ return ;
183+ }
184+ }
185+
115186 if ( ElectronNetRuntime . OnAppReadyCallback == null )
116187 {
117188 Console . WriteLine ( "Warning: Non OnReadyCallback provided in UseElectron() setup." ) ;
0 commit comments