@@ -328,6 +328,13 @@ public async Task Run()
328328 if ( _launchMode != LaunchMode . Player )
329329 await mutex . ReleaseAsync ( ) ;
330330
331+ if ( _launchMode == LaunchMode . Player )
332+ {
333+ // await because some peoples pc are so ass that roblox opens before this finishes causing an error due to the event
334+ if ( App . Settings . Prop . MultiInstanceLaunching )
335+ await LaunchMultiInstanceWatcher ( ) ;
336+ }
337+
331338 if ( ! App . LaunchSettings . NoLaunchFlag . Active && ! _cancelTokenSource . IsCancellationRequested )
332339 {
333340 if ( ! App . LaunchSettings . QuietFlag . Active )
@@ -716,7 +723,7 @@ private bool IsEligibleForBackgroundUpdate()
716723 }
717724 }
718725
719- private static void LaunchMultiInstanceWatcher ( )
726+ private static async Task LaunchMultiInstanceWatcher ( )
720727 {
721728 const string LOG_IDENT = "Bootstrapper::LaunchMultiInstanceWatcher" ;
722729
@@ -737,7 +744,7 @@ private static void LaunchMultiInstanceWatcher()
737744 App . Logger . WriteLine ( LOG_IDENT , "Mutex ROBLOX_singletonEvent already exists, skipping creation" ) ;
738745 }
739746 else
740- {
747+ {
741748 _multiInstanceMutex2 = new Mutex ( true , "ROBLOX_singletonEvent" ) ;
742749 App . Logger . WriteLine ( LOG_IDENT , "Created multi-instance mutex: ROBLOX_singletonEvent" ) ;
743750 }
@@ -780,82 +787,61 @@ private static void LaunchMultiInstanceWatcher()
780787 using EventWaitHandle initEventHandle = new EventWaitHandle ( false , EventResetMode . AutoReset , "Bloxstrap-MultiInstanceWatcherInitialisationFinished" ) ;
781788 Process . Start ( Paths . Process , "-multiinstancewatcher" ) ;
782789
783- bool initSuccess = initEventHandle . WaitOne ( TimeSpan . FromSeconds ( 2 ) ) ;
784- if ( initSuccess )
785- App . Logger . WriteLine ( LOG_IDENT , "Initialisation finished signalled, continuing." ) ;
786- else
787- App . Logger . WriteLine ( LOG_IDENT , "Did not receive the initialisation finished signal, continuing." ) ;
790+ await Task . Run ( ( ) => initEventHandle . WaitOne ( TimeSpan . FromSeconds ( 2 ) ) ) ;
791+
792+ App . Logger . WriteLine ( LOG_IDENT , "Multi-instance watcher initialization completed" ) ;
788793 }
789794
790795 // Cleanup starts in watcher not here
791796 public void CleanupMultiInstanceResources ( )
792797 {
793798 const string LOG_IDENT = "Bootstrapper::CleanupMultiInstanceResources" ;
794799
795- bool processesStillRunning = false ;
796800 try
797801 {
798- int robloxProcessCount = Process . GetProcesses ( ) . Count ( x => x . ProcessName == "RobloxPlayerBeta" ) ;
799-
800- bool bloxstrapMutexExists = false ;
801- try
802- {
803- using ( var mutex = Mutex . OpenExisting ( MutexName ) )
804- {
805- bloxstrapMutexExists = true ;
806- }
807- }
808- catch ( WaitHandleCannotBeOpenedException )
809- {
810- bloxstrapMutexExists = false ;
811- }
812- catch ( Exception mutexEx )
813- {
814- App . Logger . WriteLine ( LOG_IDENT , $ "Error checking for launching mutex: { mutexEx . Message } ") ;
815- bloxstrapMutexExists = true ;
816- }
802+ int count = Process . GetProcesses ( ) . Count ( x => x . ProcessName is "RobloxPlayerBeta" ) ;
803+ count -= 1 ;
817804
818- processesStillRunning = ( robloxProcessCount > 0 ) || bloxstrapMutexExists ;
819-
820- if ( processesStillRunning )
821- {
822- string reason = bloxstrapMutexExists ? "Launching mutex exists" : $ "Roblox processes still running ({ robloxProcessCount } instances)";
823- App . Logger . WriteLine ( LOG_IDENT , $ "{ reason } , skipping mutex disposal") ;
824- }
825- else
805+ if ( count > 0 )
826806 {
827- App . Logger . WriteLine ( LOG_IDENT , "No Roblox processes running and no launching mutex found, proceeding with mutex disposal" ) ;
807+ App . Logger . WriteLine ( LOG_IDENT , $ "Skipping cleanup - { count } Roblox process(es) still running") ;
808+ return ;
828809 }
829810 }
830811 catch ( Exception ex )
831812 {
832- App . Logger . WriteLine ( LOG_IDENT , $ "Error checking for running processes: { ex . Message } " ) ;
833- processesStillRunning = true ;
813+ App . Logger . WriteException ( LOG_IDENT , ex ) ;
814+ return ;
834815 }
835816
836- if ( ! processesStillRunning )
817+ bool launchingMutex = Utilities . DoesMutexExist ( MutexName ) ;
818+
819+ if ( launchingMutex )
837820 {
838- try
839- {
840- if ( _multiInstanceMutex1 != null )
841- {
842- _multiInstanceMutex1 . Dispose ( ) ;
843- _multiInstanceMutex1 = null ;
844- App . Logger . WriteLine ( LOG_IDENT , "Disposed ROBLOX_singletonMutex" ) ;
845- }
821+ App . Logger . WriteLine ( LOG_IDENT , "Skipping cleanup, currently launching roblox" ) ;
822+ return ;
823+ }
846824
847- if ( _multiInstanceMutex2 != null )
848- {
849- _multiInstanceMutex2 . Dispose ( ) ;
850- _multiInstanceMutex2 = null ;
851- App . Logger . WriteLine ( LOG_IDENT , "Disposed ROBLOX_singletonEvent" ) ;
852- }
825+ try
826+ {
827+ if ( _multiInstanceMutex1 != null )
828+ {
829+ _multiInstanceMutex1 . Dispose ( ) ;
830+ _multiInstanceMutex1 = null ;
831+ App . Logger . WriteLine ( LOG_IDENT , "Disposed ROBLOX_singletonMutex" ) ;
853832 }
854- catch ( Exception ex )
833+
834+ if ( _multiInstanceMutex2 != null )
855835 {
856- App . Logger . WriteLine ( LOG_IDENT , $ "Error disposing mutexes: { ex . Message } ") ;
836+ _multiInstanceMutex2 . Dispose ( ) ;
837+ _multiInstanceMutex2 = null ;
838+ App . Logger . WriteLine ( LOG_IDENT , "Disposed ROBLOX_singletonEvent" ) ;
857839 }
858840 }
841+ catch ( Exception ex )
842+ {
843+ App . Logger . WriteLine ( LOG_IDENT , $ "Error disposing mutexes: { ex . Message } ") ;
844+ }
859845
860846 if ( App . Settings . Prop . Error773Fix )
861847 {
@@ -952,12 +938,6 @@ private async void StartRoblox()
952938
953939 SetStatus ( Strings . Bootstrapper_Status_Starting ) ;
954940
955- if ( _launchMode == LaunchMode . Player )
956- {
957- if ( App . Settings . Prop . MultiInstanceLaunching )
958- LaunchMultiInstanceWatcher ( ) ;
959- }
960-
961941 string [ ] Names = { App . RobloxPlayerAppName , App . RobloxAnselAppName , App . RobloxStudioAppName } ;
962942 string ResolvedName = null ! ;
963943
0 commit comments