tags:

views:

32

answers:

2
+1  Q: 

multiple dialog

I have limited my application to one instance(in vc++ 6.0), if you open second time the message will display like "Another instace is already running".ok thats fine.

but if you open second time,third time the message will repeatedly display with new messagebox. I want to display one MessageBox Repeatedly same .Otherwise the user has to close the messagebox number of times that he has requested. here is the my code:-

BOOL init()
{
   HANDLE mutex = CreateMutex(NULL, FALSE, "mutexname");
   if(mutex == NULL)
   {
      return FALSE;
   }

   if (GetLastError() == ERROR_ALREADY_EXISTS)
   { 

       MessageBox("Another instance is already running.");
       return FALSE;
   }

   return TRUE;
}
+2  A: 

You'd be better off not displaying the message box at all; just bring the other window to the front.

See this CodeGuru page, for example.

Roger Lipscombe
A: 

Surely the really simple answer is to repeat the method you have already used.

The code checks the mutex to see if it already exists. If it does (another run of the program), it now creates another named mutex and checks if that exists. If it does, the program simply goes away, becasue it knows the message box is up. Otherwise, it puts up the message box and destroys the mutex when the message box returns.

BOOL init()
{
   HANDLE mutex = CreateMutex(NULL, FALSE, "mutexname");
   if(mutex == NULL)
   {
      return FALSE;
   }

   if (GetLastError() == ERROR_ALREADY_EXISTS)
   { 

       HANDLE mess_mutex = CreateMutex(NULL, FALSE, "message_mutexname");
       if(mess_mutex == NULL)
       {
          return FALSE;
       }
       if (GetLastError() != ERROR_ALREADY_EXISTS)
       { 
           MessageBox("Another instance is already running.");
       }            
       CloseHandle(mess_mutex);
       return FALSE;
   }

   return TRUE;
}
Simon Parker