Lazarus中文社区

 找回密码
 立即注册(注册审核可向QQ群索取)

QQ登录

只需一步,快速开始

Lazarus IDE and 组件 下载地址版权申明
查看: 2831|回复: 7

Lazaurs相同程序实现互斥的方法

[复制链接]

该用户从未签到

发表于 2012-5-10 12:37:16 | 显示全部楼层 |阅读模式
原本用以下方式在Delphi下实现过:
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   CreateMutex(nil, false, '生产线管理系统'); //建立互斥对象
  4.   if GetLastError = ERROR_ALREADY_EXISTS then //说明已经有此实例,已经运行
  5.   begin
  6.     messagebox(0, '该程序已经有一个在运行中!', '警告', 0); //提示程序已运行
  7.     halt; //退出
  8.   end;
  9. end;
复制代码
用上面的方式在Lazaurs下不能实现,所以就找到一个新的办法,现经过测试,有效!
方法共享如下:
  1. program project1;
  2. {$mode objfpc}{$H+}
  3. uses
  4.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  5.   cthreads,
  6.   {$ENDIF}{$ENDIF}
  7.   Interfaces, // this includes the LCL widgetset
  8.   Forms, Unit1,
  9.   windows
  10.   { you can add units after this };
  11. {$R *.res}
  12. var
  13.   WindowHandler : THandle;
  14. begin
  15.   WindowHandler := FindWindow(nil, 'project1');
  16.   if WindowHandler > 0 then begin
  17.     SetForegroundWindow(WindowHandler);
  18.     Halt;
  19.   end;
  20.   RequireDerivedFormResource := True;
  21.   Application.Initialize;
  22.   Application.CreateForm(TForm1, Form1);
  23.   Application.Run;
  24. end.
复制代码

如果你有更好的办法,欢迎交流!
回复

使用道具 举报

该用户从未签到

 楼主| 发表于 2012-5-10 12:49:08 | 显示全部楼层
以下方式在Lazarus下也可以实现互斥:
  1. program project1;
  2. {$mode objfpc}{$H+}
  3. uses
  4.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  5.   cthreads,
  6.   {$ENDIF}{$ENDIF}
  7.   Interfaces, // this includes the LCL widgetset
  8.   Forms, Unit1,
  9.   windows
  10.   { you can add units after this };
  11. {$R *.res}
  12. const
  13.   MemFileSize = 1024;
  14.   MemFileName = 'one_inst_TST';
  15. var
  16.   MemHnd, MainFormHnd : HWND; // DWord;
  17.   FFileView: Pointer;
  18. begin
  19.   MemHnd := CreateFileMapping(HWND($FFFFFFFF),nil, PAGE_READWRITE, 0, MemFileSize, MemFileName);
  20.   if GetLastError<>ERROR_ALREADY_EXISTS then begin
  21.     FFileView:=nil;
  22.     Application.Initialize;
  23.     Application.CreateForm(TForm1, Form1);
  24.     MainFormHnd:= Form1.Handle;
  25.     // 写MainForm的处理文件
  26.     FFileView := MapViewOfFile(MemHnd,FILE_MAP_ALL_ACCESS,0,0,0);
  27.     if FFileView <> nil then begin
  28.       CopyMemory( FFileView, @MainFormHnd, 4);
  29.       UnmapViewOfFile(FFileView);
  30.     end;
  31.     Application.CreateForm(TForm1, Form1);
  32.     Application.Run;
  33.   end
  34.   else begin
  35.     // 从以前的实例中创建的文件阅读MainForm的句柄
  36.     FFileView := MapViewOfFile(MemHnd,FILE_MAP_READ,0,0,0);
  37.     CopyMemory( @MainFormHnd, FFileView, 4);
  38.     SetForegroundWindow(MainFormHnd); // 激活以前
  39.   end;
  40.   CloseHandle(MemHnd);
  41. end.
复制代码
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2012-5-10 13:36:16 | 显示全部楼层
保证一个程序一个实例,相同程序互斥的组件:
http://wiki.lazarus.freepascal.org/UniqueInstance
Easy of use: just drop a component in the main form
UniqueInstance provides an easy way to force only one instance per application running at same time.
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-5-10 14:02:12 | 显示全部楼层
创建一个文件,每次启动判断一下,
有就不开新进程,
没有就干。
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-5-10 15:02:53 | 显示全部楼层
下面是我在Lazarus里用的代码:

  1. program project1;
  2. {$mode objfpc}{$H+}
  3. uses
  4.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  5.   cthreads,
  6.   {$ENDIF}{$ENDIF}
  7.   Interfaces, // this includes the LCL widgetset
  8.   Forms, Unit1, LResources, windows;
  9. {$IFDEF WINDOWS}{$R project1.rc}{$ENDIF}
  10. var
  11.     hw : HWND;
  12.     gt : Integer;
  13. begin
  14.   {$I project1.lrs}
  15.   Application.Title:='www.fpccn.com';
  16.   Application.Initialize;
  17.   hw := CreateMutex(nil,False,'www.fpccn.com'); {创建互斥体对象}
  18.   gt := GetLastError;
  19.   if gt <> Error_ALREADY_EXISTS then                    {如果没有发现互斥体对象}
  20.   begin
  21.     Application.CreateForm(TForm1, Form1);
  22.     Application.Run;
  23.   end
  24.   else begin
  25.          Application.Terminate;
  26.          ReleaseMutex(hw);                                        {释放互斥体}
  27.        end;
  28. end.   
复制代码
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2012-5-13 00:44:30 | 显示全部楼层
最好能写出没有依赖平台的代码,上面的都依赖windows了
回复 支持 反对

使用道具 举报

该用户从未签到

 楼主| 发表于 2012-5-13 09:37:51 | 显示全部楼层
linux wince都可以
回复 支持 反对

使用道具 举报

该用户从未签到

发表于 2013-11-12 11:07:50 | 显示全部楼层

保证一个程序一个实例,相同程序互斥的组件:
http://wiki.lazarus.freepascal.org/UniqueInstance
Easy of use: just drop a component in the main form
UniqueInstance provides an easy way to force only one instance per application running at same time.


赞一个,这个好.好强
回复 支持 反对

使用道具 举报

*滑块验证:

本版积分规则

QQ|手机版|小黑屋|Lazarus中国|Lazarus中文社区 ( 鄂ICP备16006501号-1 )

GMT+8, 2025-5-2 23:22 , Processed in 0.035201 second(s), 10 queries , Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表