如何使delphi程序生成的exe文件在一台计算机上同时只能运行一个,而不能同时运行两个或多个?

zcs_1 2002-08-26 07:38:03
我是新手,希望能给出代码,万分感谢!
...全文
325 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
softssh 2002-08-26
  • 打赏
  • 举报
回复
同意自定义消息
利用消息机制不错!
紫郢剑侠 2002-08-26
  • 打赏
  • 举报
回复
方法比较多,除上面诸大侠给的FINDWINDOW,创建互斥体外, 还可以使用全局元子变量,设置内存变量,建立临时文件作为运行标志等。
BambooCaep 2002-08-26
  • 打赏
  • 举报
回复
刚才有个字符串没有改掉。
var hMutex: THandle;
begin
hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'YourString');
if hMutex <> 0 then Exit;
hMutex := CreateMutex(nil, False, 'YourString');
if hMutex = 0 then Exit;
try
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
finally
ReleaseMutex(hMutex);
end;
end.
santwy 2002-08-26
  • 打赏
  • 举报
回复
创建互坼体

var hMutex: THandle;
begin
hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'YourString');
if hMutex <> 0 then Exit;
hMutex := CreateMutex(nil, False, 'BambooChatHotKey');
if hMutex = 0 then Exit;
try
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
finally
ReleaseMutex(hMutex);
end;
end.
santwy 2002-08-26
  • 打赏
  • 举报
回复
创建互坼体

var hMutex: THandle;
begin
hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'YourString');
if hMutex <> 0 then Exit;
hMutex := CreateMutex(nil, False, 'BambooChatHotKey');
if hMutex = 0 then Exit;
try
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
finally
ReleaseMutex(hMutex);
end;
end.
BambooCaep 2002-08-26
  • 打赏
  • 举报
回复
var hMutex: THandle;
begin
hMutex := OpenMutex(MUTEX_ALL_ACCESS, True, 'YourString');
if hMutex <> 0 then Exit;
hMutex := CreateMutex(nil, False, 'BambooChatHotKey');
if hMutex = 0 then Exit;
try
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
finally
ReleaseMutex(hMutex);
end;
end.
zcs_1 2002-08-26
  • 打赏
  • 举报
回复
谢谢各位大虾帮忙,我分别测试一下代码,问题解决后立即给分。
mo_hui 2002-08-26
  • 打赏
  • 举报
回复
if FindWindow('TApplication', '****系统') <> 0 then Halt;
jyqkr 2002-08-26
  • 打赏
  • 举报
回复
var Mutex: THandle;
begin
Mutex := CreateMutex(nil, True, 'SingleApp'); //SingleApp,一个比较特殊的名字
if GetLastError <> ERROR_ALREADY_EXISTS then
begin
Application.Initialize;
end;
My_first 2002-08-26
  • 打赏
  • 举报
回复
把这段程序加到 project-->view source
var
//避免程序的二次运行;
h: HWND;
begin
Application.Initialize;
h := FindWindowEx(0, 0, 'TApplication', '管理系统');
if h <> 0 then Application.Terminate;
Application.Title := '管理系统';

Application.Title := '管理系统';
Application.CreateForm(TMianForm, MianForm);
LoginForm.Free;
Application.Run;
shuixin13 2002-08-26
  • 打赏
  • 举报
回复
program AUTORUN;

uses
Forms,Windows,Messages,
FRM_AUTORUN in 'FRM_AUTORUN.pas' {LOGO};
const// 自定义一个消息,用来恢复窗口
CM_RESTORE=WM_USER+$1000;
APPNAME='安装程序';
VAR RvHandle:hWnd;
{$R *.RES}
begin
RvHandle :=FindWindow(APPNAME, NIL); // 根据窗体类名查找窗口句柄
if RvHandle > 0 then // 如果找到则发送自定义的消息并退出
begin
PostMessage(RvHandle,CM_RESTORE,0,0);
SetForegroundWindow(RvHandle);
exit;
end ;
Application.Initialize;
Application.Title := '网络版安装引导界面';
Application.CreateForm(TLOGO, LOGO);
Application.Run;
end.


/////////////////////////////////////////////////////

unit FRM_AUTORUN;

interface

uses
Windows,Forms,jpeg,Controls, StdCtrls, ExtCtrls, Classes,Messages,
shellapi, SysUtils;
const// 自定义一个消息,用来恢复窗口
CM_RESTORE=WM_USER+$1000;
APPNAME='安装程序';
type
TLOGO = class(TForm)


private
{ Private declarations }

public
{ Public declarations }
procedure CreateParams(var Params: TCreateParams); override;
Procedure RestoreRequest(var message: TMessage); message CM_RESTORE ;
end;

var
LOGO: TLOGO;

implementation

{$R *.DFM}
procedure Tlogo.CreateParams(var Params: TCreateParams);
begin
// 设置窗体的类名
inherited CreateParams(Params);
Params.WinClassName := APPNAME;
end ;

procedure Tlogo.RestoreRequest(var message: TMessage);
begin
// 接到自定义的消息后,如果处于最小化状态则恢复,否则放置到桌面的最前面
//messageBox (Handle , '程序" ' + APPNAME+ ' "已经运行了。' ,'信息' , MB_OK + MB_ICONINFORMATION + MB_SYSTEMMODAL) ;
if IsIconic(Application.Handle) = TRUE then
Application.Restore
else
Application.BringToFront;
end;


end.


程序运行时会进行“根据窗体类名查找窗口句柄”操作,找到了将那个程序
设为前台,并关闭自已,
如没找到,则继续运行。

也就是实现一个程序只能运行 一个实例,
zcs_1 2002-08-26
  • 打赏
  • 举报
回复
JonHua(玉龙) ,你的代码不行啊:

提示Error_ALREADY_EXISTS为未定义的变量,能否再帮忙看看。
JonHua 2002-08-26
  • 打赏
  • 举报
回复
var
ret:integer;
if ret = Error_ALREADY_EXISTS then
Application.MessageBox('系统已在运行!','提示',mb_OK);
cobi 2002-08-26
  • 打赏
  • 举报
回复
用注册表控制简单
dongxsoft 2002-08-26
  • 打赏
  • 举报
回复
用FindWindow()

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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