wxSingleInstanceChecker 类允许检查是否只有一个程序实例正在运行。
因此,首先应该创建此类的对象。只要此对象处于活动状态,从其他进程调用wxSingleInstanceChecker::IsAnotherRunning()将返回true。
由于对象的寿命应该尽可能大,所以可将其创建为全局对象或是在wxApp::OnInit中创建。例如:
//头文件
#include <wx/snglinst.h>
bool MyApp::OnInit()
{
m_checker = new wxSingleInstanceChecker; //另也可为类设置文件名,路径提供唯一性
if ( m_checker->IsAnotherRunning() )
{
wxLogError(_("Another program instance is already running, aborting."));
delete m_checker; // OnExit() won't be called if we return false
m_checker = NULL;
return false;
}
... more initializations ...
return true;
}
int MyApp::OnExit()
{
delete m_checker;
return 0;
}