怎样避免重复运行一个程序?
好象只有几行,好久没写想不起来了

解决方案 »

  1.   

    CreateMutex(nil, True, 'MyApp');
    if GetLastError = ERROR_ALREADY_EXISTS then
    begin
      MessageBox(0, '程序已经在运行', '我的程序', MB_ICONERROR);
    end
    else
    begin
        Application.Initialize;
        Application.CreateForm(TMainForm, MainForm);
        Application.Run;
    end;
      

  2.   

    uses windows;
    ...
    var MutexHandle:Thandle; 
    begin 
    Mutexhandle:=windows.CreateMutex(nil,true,'能代表程序特点的字符串');
    if Mutexhandle<>0 then
    begin
    if getlasterror=windows.ERROR_ALREADY_EXISTS then
    begin
    closehandle(Mutexhandle);
    halt;
    end;
    end;Application.Initialize;
    Application.CreateForm(Tfmmain, fmmain);
    Application.Run;
    end.