代码之家  ›  专栏  ›  技术社区  ›  Simsons

如何只运行一个应用程序实例

  •  12
  • Simsons  · 技术社区  · 14 年前

    我有一个应用程序,它使用套接字连接从另一个应用程序发送和接收数据。创建套接字时使用端口4998。

    这就是我的问题所在。一旦启动应用程序,套接字就开始使用端口4998。因此,如果我想再次执行应用程序,那么我会得到套接字绑定错误。

    所以我想将我的应用程序实例限制为一个。这意味着,如果应用程序已经在运行,并且有人试图通过单击exe或快捷方式图标再次运行应用程序,则不应运行该程序,而是应将现有应用程序置于顶层。

    5 回复  |  直到 14 年前
        1
  •  12
  •   acoolaum    14 年前

    您可以使用命名的互斥体。

    代码示例来自 article :

    WINAPI WinMain(
      HINSTANCE, HINSTANCE, LPSTR, int)
    {
      try {
        // Try to open the mutex.
        HANDLE hMutex = OpenMutex(
          MUTEX_ALL_ACCESS, 0, "MyApp1.0");
    
        if (!hMutex)
          // Mutex doesn’t exist. This is
          // the first instance so create
          // the mutex.
          hMutex = 
            CreateMutex(0, 0, "MyApp1.0");
        else
          // The mutex exists so this is the
          // the second instance so return.
          return 0;
    
        Application->Initialize();
        Application->CreateForm(
          __classid(TForm1), &Form1);
        Application->Run();
    
        // The app is closing so release
        // the mutex.
        ReleaseMutex(hMutex);
      }
      catch (Exception &exception) {
        Application->
          ShowException(&exception);
      }
      return 0;
    }
    
        2
  •  5
  •   krishna_kp    12 年前

    /* 我已经找到必要的编辑工作要做。添加了一些需要的额外代码和编辑。现在的那个对我来说是完美的。谢谢你,基里尔诉莱德文斯基和雷米·勒博的帮助!!

    */

    bool CheckOneInstance()
    {
    
        HANDLE  m_hStartEvent = CreateEventW( NULL, FALSE, FALSE, L"Global\\CSAPP" );
    
        if(m_hStartEvent == NULL)
        {
        CloseHandle( m_hStartEvent ); 
            return false;
        }
    
    
        if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
    
            CloseHandle( m_hStartEvent ); 
            m_hStartEvent = NULL;
            // already exist
            // send message from here to existing copy of the application
            return false;
        }
        // the only instance, start in a usual way
        return true;
    }
    

    /* 上面的代码即使试图从另一个登录打开第二个实例,而第一个登录打开时其实例仍在运行。 */

        3
  •  5
  •   Edward Brey    9 年前

    当应用程序初始化时,创建互斥体。如果它已经存在,请找到现有的应用程序并将其置于前台。如果应用程序的主窗口有固定的标题,则很容易找到 FindWindow .

    m_singleInstanceMutex = CreateMutex(NULL, TRUE, L"Some unique string for your app");
    if (m_singleInstanceMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS) {
        HWND existingApp = FindWindow(0, L"Your app's window title");
        if (existingApp) SetForegroundWindow(existingApp);
        return FALSE; // Exit the app. For MFC, return false from InitInstance.
    }
    
        4
  •  3
  •   Kirill V. Lyadvinsky    14 年前

    在开始时创建命名事件并检查结果。如果事件已经存在,则关闭应用程序。

    BOOL CheckOneInstance()
    {
        m_hStartEvent = CreateEventW( NULL, TRUE, FALSE, L"EVENT_NAME_HERE" );
        if ( GetLastError() == ERROR_ALREADY_EXISTS ) {
            CloseHandle( m_hStartEvent ); 
            m_hStartEvent = NULL;
            // already exist
            // send message from here to existing copy of the application
            return FALSE;
        }
        // the only instance, start in a usual way
        return TRUE;
    }
    

    关闭 m_hStartEvent 在应用程序退出时。

        5
  •  0
  •   Ana Betts    14 年前

    您是否已经有了检查应用程序是否正在运行的方法?谁需要互斥,如果端口已经被占用,你就知道应用程序正在运行!