我用 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/