您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Delphi >> 请教大家关于在delphi中防止程序多次运行的方法???

请教大家关于在delphi中防止程序多次运行的方法???

来源:网络整理     时间:2016/7/8 13:16:55     关键词:

关于网友提出的“ 请教大家关于在delphi中防止程序多次运行的方法???”问题疑问,本网通过在网上对“ 请教大家关于在delphi中防止程序多次运行的方法???”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:

问题: 请教大家关于在delphi中防止程序多次运行的方法???
描述:

请教大家关于在delphi中防止程序多次运行的方法,希望大家能够给我说说原理,最好还能够提供源代码,先谢谢大家了!!!


解决方案1:

unit Unit1;
interface
uses
  Windows,...
type
...
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
 ...
implementation
var hnd:THandle;
{$R *.DFM}
...
...
//防止程序被执行两次
initialization
hnd:=CreateMutex(nil,True,'irgendwaseinmaliges');
if GetLastError=ERROR_ALREADY_EXISTS then
  begin
    MessageBox(Application.handle,'程序已运行!','提示',MB_IconWarning+mb_Ok);
    Halt;
  end;
finalization
if hnd <> 0 then CloseHandle(hnd);
end.

解决方案2:

procedure TForm1.FormCreate(Sender: TObject);
var
  ZAppName: array[0..127] of char;
  Hold: String;
  Found: HWND;
begin
  Hold := Application.Title;
  Application.Title := 'OnlyOne'
     + IntToStr(HInstance); // 暂时修改窗口标题
  StrPCopy(ZAppName, Hold); // 原窗口标题
  Found := FindWindow(nil, ZAppName); // 查找窗口
  Application.Title := Hold; // 恢复窗口标题
  if Found<>0 then begin
    // 若找到则激活已运行的程序并结束自身
    ShowWindow(Found, SW_RESTORE);
    Application.Terminate;
  end;
end;

解决方案3:

我相信这个是目前最好的实现单实例运行的代码:
功能:实现只有一个程序运行,并且当你想运行第二次程序时,只会激活原来的程序,下面是我的文章的地址:
http://blog.csdn.net/linzhengqun/archive/2005/12/29/564646.aspx

解决方案4:

最好是创建互斥对象
var
  Exist:DWORD;
begin
  Application.Initialize;
  CreateMuteX(nil,True,'管理系统');
  Exist:=GetLastError;
  If Exist=ERROR_ALREADY_EXISTS then begin
    Application.MessageBox('管理系统程序已经运行!','信息',MB_OK+MB_ICONINFORMATION);
    Exit;
  end;

解决方案5:

用FindWindow找窗体标题,或者创建互斥对象都可以


以上介绍了“ 请教大家关于在delphi中防止程序多次运行的方法???”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2416593.html

相关图片

相关文章