@@ -40,7 +40,7 @@ public partial class FlashpointSecurePlayerGUI : Form {
4040 private readonly bool oldWindowsVersion = Environment . OSVersion . Version < new Version ( 6 , 1 ) ;
4141
4242 private bool activeX = false ;
43- WebBrowserMode webBrowserMode = null ;
43+ private bool webBrowserModeExiting = false ;
4444 private ProcessStartInfo softwareProcessStartInfo = null ;
4545 private bool softwareIsOldCPUSimulator = false ;
4646
@@ -619,7 +619,7 @@ private async Task ImportActiveXAsync(ErrorDelegate errorDelegate) {
619619 }
620620 }
621621
622- private void ActivateMode ( TemplateElement templateElement , ErrorDelegate errorDelegate ) {
622+ private async Task ActivateModeAsync ( TemplateElement templateElement , ErrorDelegate errorDelegate ) {
623623 bool createdNew = false ;
624624
625625 using ( Mutex modeMutex = new Mutex ( true , MODE_MUTEX_NAME , out createdNew ) ) {
@@ -662,11 +662,10 @@ private void ActivateMode(TemplateElement templateElement, ErrorDelegate errorDe
662662 throw new InvalidModeException ( "The address \" " + URL + "\" was not understood by the Mode." ) ;
663663 }
664664
665- webBrowserMode = new WebBrowserMode ( webBrowserURL , UseFlashActiveXControl ) {
665+ WebBrowserMode webBrowserMode = new WebBrowserMode ( WebBrowserModeExit , webBrowserURL , UseFlashActiveXControl ) {
666666 WindowState = FormWindowState . Maximized
667667 } ;
668-
669- webBrowserMode . FormClosing += webBrowserMode_FormClosing ;
668+
670669 Hide ( ) ;
671670 webBrowserMode . Show ( ) ;
672671 return ;
@@ -726,7 +725,9 @@ private void ActivateMode(TemplateElement templateElement, ErrorDelegate errorDe
726725
727726 Hide ( ) ;
728727
729- softwareProcess . WaitForExit ( ) ;
728+ await Task . Run ( delegate ( ) {
729+ softwareProcess . WaitForExit ( ) ;
730+ } ) . ConfigureAwait ( true ) ;
730731
731732 Show ( ) ;
732733 Refresh ( ) ;
@@ -809,10 +810,14 @@ private void DeactivateMode(TemplateElement templateElement, ErrorDelegate error
809810 }
810811
811812 try {
812- if ( webBrowserMode != null ) {
813- webBrowserMode . FormClosing -= webBrowserMode_FormClosing ;
814- webBrowserMode . Close ( ) ;
815- webBrowserMode = null ;
813+ webBrowserModeExiting = true ;
814+
815+ IList < WebBrowserMode > webBrowserModes = WebBrowserMode . WebBrowserModes ;
816+
817+ if ( webBrowserModes != null ) {
818+ for ( int i = 0 ; i < webBrowserModes . Count ; i ++ ) {
819+ webBrowserModes [ i ] . Close ( ) ;
820+ }
816821 }
817822 } finally {
818823 modeMutex . ReleaseMutex ( ) ;
@@ -1201,10 +1206,10 @@ await ActivateModificationsAsync(templateElement, delegate (string text) {
12011206 }
12021207
12031208 try {
1204- ActivateMode ( templateElement , delegate ( string text ) {
1209+ await ActivateModeAsync ( templateElement , delegate ( string text ) {
12051210 ShowErrorFatal ( text ) ;
12061211 throw new InvalidModeException ( "An error occured while activating the Mode." ) ;
1207- } ) ;
1212+ } ) . ConfigureAwait ( true ) ;
12081213 } catch ( InvalidModeException ex ) {
12091214 // delegate handles error
12101215 LogExceptionToLauncher ( ex ) ;
@@ -1248,6 +1253,34 @@ private void ImportStart(object sender, EventArgs e) {
12481253 private void ImportStop ( object sender , EventArgs e ) {
12491254 ControlBox = true ;
12501255 }
1256+
1257+ private void WebBrowserModeExit ( object sender , EventArgs e ) {
1258+ if ( webBrowserModeExiting ) {
1259+ return ;
1260+ }
1261+
1262+ // Set Current Directory
1263+ try {
1264+ Directory . SetCurrentDirectory ( Application . StartupPath ) ;
1265+ } catch {
1266+ // fail silently
1267+ }
1268+
1269+ // this should not cause an exception
1270+ // if it does, it means there is an infinite closing loop
1271+ // (a form is closing a form which is closing a form, and so on)
1272+ // you can debug infinite closing loops by setting breakpoints
1273+ // on all Form.Close functions
1274+ try {
1275+ Show ( ) ;
1276+ Refresh ( ) ;
1277+ Application . Exit ( ) ;
1278+ } catch ( InvalidOperationException ex ) {
1279+ // IT IS VERY IMPORTANT THIS SHOULD NEVER HAPPEN!
1280+ LogExceptionToLauncher ( ex ) ;
1281+ Environment . Exit ( - 1 ) ;
1282+ }
1283+ }
12511284
12521285 private bool loaded = false ;
12531286
@@ -1601,36 +1634,6 @@ private void FlashpointSecurePlayer_FormClosing(object sender, FormClosingEventA
16011634 }
16021635 }
16031636 }
1604-
1605- private void webBrowserMode_FormClosing ( object sender , FormClosingEventArgs e ) {
1606- // stop form closing recursion
1607- if ( webBrowserMode != null ) {
1608- webBrowserMode . FormClosing -= webBrowserMode_FormClosing ;
1609- webBrowserMode = null ;
1610- }
1611-
1612- // Set Current Directory
1613- try {
1614- Directory . SetCurrentDirectory ( Application . StartupPath ) ;
1615- } catch {
1616- // fail silently
1617- }
1618-
1619- // this should not cause an exception
1620- // if it does, it means there is an infinite closing loop
1621- // (a form is closing a form which is closing a form, and so on)
1622- // you can debug infinite closing loops by setting breakpoints
1623- // on all Form.Close functions
1624- try {
1625- Show ( ) ;
1626- Refresh ( ) ;
1627- Application . Exit ( ) ;
1628- } catch ( InvalidOperationException ex ) {
1629- // IT IS VERY IMPORTANT THIS SHOULD NEVER HAPPEN!
1630- LogExceptionToLauncher ( ex ) ;
1631- Environment . Exit ( - 1 ) ;
1632- }
1633- }
16341637 }
16351638}
16361639
0 commit comments