c++ - How to Run Only One Instance of Application -


i have application uses socket connection send , receive data application. while creating socket uses port 4998 .

that problem lie. once start application socket starts using port 4998. if want execute application again socket binding error.

so want limit application instance one. means if application running , 1 tries run application again clicking exe or shortcut icon shouldn't run program, instead should bring existing application top.

you may used named mutex.

code sample article:

winapi winmain(   hinstance, hinstance, lpstr, int) {   try {     // try open mutex.     handle hmutex = openmutex(       mutex_all_access, 0, "myapp1.0");      if (!hmutex)       // mutex doesn’t exist.       // first instance create       // mutex.       hmutex =          createmutex(0, 0, "myapp1.0");     else       // mutex exists       // second instance return.       return 0;      application->initialize();     application->createform(       __classid(tform1), &form1);     application->run();      // app closing release     // mutex.     releasemutex(hmutex);   }   catch (exception &exception) {     application->       showexception(&exception);   }   return 0; } 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -