您的位置:首页 > 其它

让程序只运行一个实例的方法

2011-03-20 16:25 344 查看
//首先在InitInstance()函数中创建一个同步对象,如果(GetLastError()返回ERROR_ALREADY_EXISTS就表明存在
  //一个应用实例了,返回false取消
  // 创建同步对象

//By cryfish
hMutexOneInstantance=CreateMutex(NULL,TRUE,_T("PreventSecondInstance"));
if(GetLastError()==ERROR_ALREADY_EXISTS)
bFound=TRUE;
if(hMutexOneInstantance)
ReleaseMutex(hMutexOneInstantance);
if (bFound==TRUE)
{ ::AfxMessageBox("You have Run one");
return false;
}

//首先在InitInstance()函数中创建一个同步对象,如果(GetLastError()返回ERROR_ALREADY_EXISTS就表明存在
  //一个应用实例了,返回false取消
  // 创建同步对象

//By cryfish
hMutexOneInstantance=CreateMutex(NULL,TRUE,_T("PreventSecondInstance"));
if(GetLastError()==ERROR_ALREADY_EXISTS)
bFound=TRUE;
if(hMutexOneInstantance)
ReleaseMutex(hMutexOneInstantance);
if (bFound==TRUE)
{ ::AfxMessageBox("You have Run one");
return false;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: