Your startup code is still incorrect.
Compare the timings of the following code snippets
Good
BadCode:int main(array<System::String ^> ^args) { bool mutexWasCreated; Console::WriteLine( System::String::Format( L"Starting client: {0}", DateTime::Now ) ); Mutex^ m = gcnew Mutex( false, "WcfShareTestServerLaunchedEvent", mutexWasCreated ); // Block other processes until startup has completed m->WaitOne( ); // Start up the server if( mutexWasCreated ) { Console::WriteLine( System::String::Format( L"Starting server - pid: {0}", Process::GetCurrentProcess( )->Id ) ); // Simulate startup up of WCF server (pause 10 seconds) Thread::Sleep( 10000 ); } // Allow access to other processes after startup has finished m->ReleaseMutex( ); Console::WriteLine( System::String::Format( L"Using server - pid: {0}", Process::GetCurrentProcess( )->Id ) ); Console::WriteLine( System::String::Format( L"Client has access: {0}", DateTime::Now ) ); Console::ReadLine( ); return 0; }
Bottom line, is that setting TakeOwnership value to True, isn't sufficient to block the opening of the mutex.Code:int main(array<System::String ^> ^args) { bool mutexWasCreated; Console::WriteLine( System::String::Format( L"Starting client: {0}", DateTime::Now ) ); Mutex^ m = gcnew Mutex( true, "WcfShareTestServerLaunchedEvent", mutexWasCreated ); // Block other processes until startup has completed // m->WaitOne( ); // Start up the server if( mutexWasCreated ) { Console::WriteLine( System::String::Format( L"Starting server - pid: {0}", Process::GetCurrentProcess( )->Id ) ); // Simulate startup up of WCF server (pause 10 seconds) Thread::Sleep( 10000 ); } // Allow access to other processes after startup has finished // m->ReleaseMutex( ); Console::WriteLine( System::String::Format( L"Using server - pid: {0}", Process::GetCurrentProcess( )->Id ) ); Console::WriteLine( System::String::Format( L"Client has access: {0}", DateTime::Now ) ); Console::ReadLine( ); return 0; }




Reply With Quote
