我采用的方法是程序运行时先查找有没有相同的运行了,如果有,就立刻退出程序.修改dpr项目文件,修改begin和end之间的代码如下:
begin
Application.Initialize;
if (FindWindow(nil,'Project1')=0) then
begin
//application.ShowMainForm:=false;
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end.但使用FindWindow这个方法时,第二个参数是选择'Project1'还是'Form1'?
在使用'Project1'时,程序都没有启动,也就是说没有执行IF里面的代码,可任务管理器中并没有'Project1'这个进程
使用'Form1'时,可以运行多个相同程序.
哪位高手能否解决下!
begin
Application.Initialize;
if (FindWindow(nil,'Project1')=0) then
begin
//application.ShowMainForm:=false;
Application.CreateForm(TForm1, Form1);
Application.Run;
end;
end.但使用FindWindow这个方法时,第二个参数是选择'Project1'还是'Form1'?
在使用'Project1'时,程序都没有启动,也就是说没有执行IF里面的代码,可任务管理器中并没有'Project1'这个进程
使用'Form1'时,可以运行多个相同程序.
哪位高手能否解决下!
解决方案 »
- 如何不使用ADO对ACCESS数据库进行存取?
- 怎样将image字段的内容保存到图象文件中?
- 用delphi7怎样实现对数据库的备份呢??
- 关于服务程序使用Ado数据库的问题?
- TObjectList的使用方法?
- 求助~
- 在keypress中如何判断为上下箭头键
- 大哥们,在ListView中,如何设置每个元素的长度啊?
- 点击Form上的一个button,不用delphi自带的onclick事件﹐而是利用windows的消息来处理﹐请问如何处理??????
- 请问方家,如何在Interbase数据库中使用GeneratorField?
- 关于使用WebBrowser打开 ppt的问题,急寻高手!
- 急求商品销售管理系统~~~~~~~~~~~~~~
别用这个方法吧,用互斥体吧
...........
var
hMutex: THandle;function MakeMutex: Boolean;
begin
hMutex := CreateMutex(nil, False, 'Form1Form1Form1Form1');
Result := (GetLastError = 0) and (hMutex <> 0);
if not Result then
FreeMutex;
end;procedure FreeMutex;
begin
if hMutex <> 0 then
begin
ReleaseMutex(hMutex);
hMutex := 0;
end;
end;begin
if MakeMutex then
begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
FreeMutex;
end
能否还详细介绍下CreateMutex方法!
hMutex:HWND;
ret:integer;
begin
application.initialize;
application.title:='project name';
hMutex:=createMutex(nil,False,'project name'); //创建互斥对象
ret:=getlastError;
if ret<>ERROR_ALREADY_EXISTS Then //没有发现互斥对象
begin
application.createform(Tform1,form1);
application.run;
end
else
application.messagebox('程序已在运行!','project name',mb_ok);
releaseMutex(hMutex); //释放互斥对象
end;
CreateMutex函数原型:
function createmutex(lpMutexAttributes:PSecurity Attributes;bInitialOwner:BOOL;lpName:PChar):THandle;
参数说明:
lpMutexAttributes 是一个PSecurity Attributes结构类型指针,可以设置为null
bInitialOwner 是否初始化互斥对象
lpName 互斥对象名称函数返回一个互斥对象的句柄。