您好,欢迎来到[编程问答]网站首页   源码下载   电子书籍   软件下载   专题
当前位置:首页 >> 编程问答 >> Delphi >> 小问题,防止程序运行多次?

小问题,防止程序运行多次?

来源:网络整理     时间:2016/7/14 6:42:47     关键词:

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

问题: 小问题,防止程序运行多次?
描述:

程序如下:
program dztxb;
uses
  Forms,
  Main in 'Main.pas' {frmMain};
{$R *.res}
var
  MutexHandle: THandle;
begin
  MutexHandle := CreateMutex(nil, TRUE, 'MysampleAppMutex');
  Application.Initialize;
  Application.Title := '测试';
  Application.CreateForm(TfrmMain, frmMain);
  Application.Run;
end.
结果总是提示“Undeclared identifier 'CreateMutex'
不知道怎么回事,请大家解决


解决方案1:

{确保只有一个运行程序}
function StartExe(S:string):boolean;
var aHandle: tHandle;
    P:array[0..79]of char;
begin
  result:=true;
  strpcopy(p,s);
  aHandle:=FindWindow(pchar('tapplication'),p);
  if ahandle<>0 then
  begin
    SetForegroundWindow(ahandle);
    setactivewindow(ahandle);
    if isiconic(ahandle) then
      showwindow(ahandle,sw_restore);
    result:=false;
  end;
end;
begin
  if startExe('进销存管理系统') then begin
    Application.Initialize;
    Application.Title :='进销存管理系统';
    Application.CreateForm(TFFirstFm, FFirstFm);
  FFirstFm.Show;
  FFirstFm.Update ;//Processes any pending paint messages immediately. 
  Application.CreateForm(TDm, Dm);
  Application.CreateForm(TMainFm, MainFm);
  Application.Run;
 //  end else halt;
  end;
写在program中》

解决方案2:

if GlobalFindAtom('exename') = 0 then
    onlyatom := GlobalAddAtom('exename')
  else
    begin
      MessageDlg('程序已经运行!', mtWarning, [mbOK], 0);
      Halt;
  end;

解决方案3:

uses windows


以上介绍了“ 小问题,防止程序运行多次?”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/2619308.html

相关图片

相关文章