如何用Delphi编写一个查询应用程序已存在实例的超级复杂长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计358个文字,预计阅读时间需要2分钟。
在Delphi中,若要重写一个VB6应用程序,并确保只运行一个实例,可以使用以下代码:
delphivar FInstance: THandle;begin FInstance :=FindWindow('YourAppClassName', 'YourAppTitle'); if FInstance=0 then begin // 应用程序未运行,正常启动 Application.Run; end else begin // 应用程序已运行,发送消息 PostMessage(FInstance, WM_SHOWWINDOW, 0, 0); end;end.
这里,你需要将 `'YourAppClassName'` 和 `'YourAppTitle'` 替换为你的应用程序的实际类名和。这样,当尝试启动第二个实例时,它会尝试将焦点切换到已运行的实例上。
我在Delphi中重写了一个VB6应用程序.它应该只有一个实例在运行.如何用最少的代码完成这项工作?在VB6中,我们只需要使用一行代码
>
If App.PrevInstance Then
‘Take some action
End If
在goggling我找到了解决方案,但它很长,我们不得不乱用.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所链接的帖子中的答案更有用/更丰富!
编辑:注意你可以把它变成一个小单元,然后在你的项目中使用那个单元.
本文共计358个文字,预计阅读时间需要2分钟。
在Delphi中,若要重写一个VB6应用程序,并确保只运行一个实例,可以使用以下代码:
delphivar FInstance: THandle;begin FInstance :=FindWindow('YourAppClassName', 'YourAppTitle'); if FInstance=0 then begin // 应用程序未运行,正常启动 Application.Run; end else begin // 应用程序已运行,发送消息 PostMessage(FInstance, WM_SHOWWINDOW, 0, 0); end;end.
这里,你需要将 `'YourAppClassName'` 和 `'YourAppTitle'` 替换为你的应用程序的实际类名和。这样,当尝试启动第二个实例时,它会尝试将焦点切换到已运行的实例上。
我在Delphi中重写了一个VB6应用程序.它应该只有一个实例在运行.如何用最少的代码完成这项工作?在VB6中,我们只需要使用一行代码
>
If App.PrevInstance Then
‘Take some action
End If
在goggling我找到了解决方案,但它很长,我们不得不乱用.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所链接的帖子中的答案更有用/更丰富!
编辑:注意你可以把它变成一个小单元,然后在你的项目中使用那个单元.

