怎么判断程序是否已经运行?

fjliningcd 2004-03-30 10:42:57
怎么判断程序是否已经运行?
保证一台机器上同时只能有一程序在运行。
...全文
71 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zihan 2004-03-30
  • 打赏
  • 举报
回复
呵呵,都说了,全部同意.
h98458 2004-03-30
  • 打赏
  • 举报
回复
if(FindWindow(NULL,"添入你的程序主窗体的caption")!=0)
{
Application->MessageBox("已启动一个相同程序!","警告",MB_OK);
Application->Terminate();
}

xhzxlqt 2004-03-30
  • 打赏
  • 举报
回复
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{
/////////////////////程序互斥代码开始
HANDLE hMutex=CreateMutex(
NULL,//nosecurityattributes
FALSE,//initiallynotowned
"mytest");//命名Mutex是全局对象
//在所有的process都可以访问到
if(hMutex==NULL||ERROR_ALREADY_EXISTS==::GetLastError())
{
Application->MessageBox("\n\n 该程序的一个实例正在运行! \n\n", "警告", MB_OK);
return FALSE;
}
//////////////////////程序互斥代码结束

Application->Initialize();
.....
}
xiaoshi0 2004-03-30
  • 打赏
  • 举报
回复
同意楼上的
free1688 2004-03-30
  • 打赏
  • 举报
回复
在工程主程序主程序异常判断加楼上代码。顶
JetKingLau 2004-03-30
  • 打赏
  • 举报
回复
这类问题提得太多了,楼主在C++Builder版搜索一下就会得到答案。
jackxm 2004-03-30
  • 打赏
  • 举报
回复
这个好办:
CreateMutex(NULL,true,"MyApp");
if(GetLastError()==ERROR_ALREADY_EXISTS)
{
ShowMessage("The program is runing.......");
Application->Terminate();
}
gqxs 2004-03-30
  • 打赏
  • 举报
回复
在工程主程序中:

WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
HANDLE hInstanceMutex = ::CreateMutex(NULL,TRUE, "PrjClient");
if(GetLastError() == ERROR_ALREADY_EXISTS)
{
if(hInstanceMutex)
CloseHandle(hInstanceMutex);
return 0;
}
try
{
Application->Initialize();
Application->CreateForm(__classid(TfrmMain), &frmMain);
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
ReleaseMutex(hInstanceMutex);
CloseHandle(hInstanceMutex);
return 0;


详细了解见
http://expert.csdn.net/Expert/topic/2694/2694694.xml?temp=.6927454
eastnofail 2004-03-30
  • 打赏
  • 举报
回复
If the function succeeds, the return value is a handle to the mutex object. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.
eastnofail 2004-03-30
  • 打赏
  • 举报
回复
创建一个互斥对象,简单方便:
HANDLE CreateMutex(
LPSECURITY_ATTRIBUTES lpMutexAttributes,
BOOL bInitialOwner,
LPCTSTR lpName
);

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧