delphi - 查找应用程序先前实例的最简单方法

标签 delphi vb6 instance single-instance

我用 Delphi 重写了一个 VB6 应用程序。它应该只有一个实例在运行。我怎样才能用最少的代码做到这一点?

在 VB6 中我们只需要使用一行代码 >

If App.PrevInstance Then 'Take some action End If

经过仔细研究,我确实找到了一个解决方案,但它很长,我们不得不弄乱 .drp 文件。

我不想那样做。

我想要更简单的东西。

最佳答案

我有一些代码:

var
    AppMutex: THandle;

{ .... }


initialization
    // Create the mutex
    AppMutex := CreateMutex(nil, True, 'MY-APPLICATION-NAME');
    if (AppMutex = 0) or (GetLastError = ERROR_ALREADY_EXISTS) then
    begin
        MessageDlg('My application is already running on this computer.'#13#10+
            'You should close the other instance before starting a new one.',mtError,
            [mbOK],0);
        Halt;
    end;

finalization
    // Close the mutex
    CloseHandle(AppMutex);

但我确信 @mghie 链接到的线程中的答案更有帮助/更丰富的功能!

编辑:请注意,您可以将其本身变成一个小单元,然后在您的项目中使用该单元。

关于delphi - 查找应用程序先前实例的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2432287/

相关文章:

rest - Delphi - 将 header 值传递给 REST 服务

multithreading - 划分工作,将任务分配给线程数组

.net - vb6 到 vb.net dll 兼容版本

vb6 - 我可以将字符串、单曲等设置为 ""和零吗?

c++ - 如何用C++获取MongoDB的当前实例?

java - 在 main() 方法中访问实例变量?

delphi - 如何使 GraphicControl 变暗

multithreading - Delphi:为什么VCL不是线程安全的?怎么会这样?

vb6 - VB6 是否有等效的#pragma 包?

objective-c - missViewControllerAnimated 是否删除 Controller 的实例