编写windows服务程序的互斥体问题
我编写了一个 Service Application 程序。
我只想让程序运行一个实例,我在程序启动工程目录中,
写了如下代码
hMutex := CreateMutex(nil, True, APP_SIMPLE_NAME);
ret := GetLastError;
if ret = ERROR_ALREADY_EXISTS then
begin
ReleaseMutex(hMutex);
MessageBox(Application.Handle, '系统提示,程序已经运行!', 'Servicedemo' ,
MB_OK + MB_DEFBUTTON1 + MB_ICONEXCLAMATION);
Exit;
end;
if StartService('Servicedemo') then
begin
Servicedemo.CreateForm(TDataMD,DataMD);
Servicedemo.CreateForm(TFrm_Main, Frm_Main);
Exit;
end;
Application.Initialize;
Application.CreateForm(TDataMD,DataMD);
Application.CreateForm(TFrm_Main, Frm_Main);
Application.Run;
if hMutex <> 0 then
begin
ReleaseMutex(hMutex);
CloseHandle(hMutex);
end;
但是当程序首先用服务在后台启动之后,再手动双击执行Servicedemo程序时,没有排斥到,还能启动N次。。。
如果先双击运行Servicedemo程序,再在后台启动Servicedemo服务,是可以排斥到得。
这是什么原因,请大侠明鉴。。。。。