【发布时间】:2014-03-25 14:42:39
【问题描述】:
我已经用 Delphi XE3 创建了一个应用程序。 我的应用程序有一个托盘图标(我为此使用 TCoolTrayIcon),所以当用户最小化它时,任务栏上没有图标,而只有托盘图标。
为了避免我的应用程序不止一个,我使用以下代码:
procedure CreateMutexes(const MutexName: String);
const
SECURITY_DESCRIPTOR_REVISION = 1;
var
SecurityDesc: TSecurityDescriptor;
SecurityAttr: TSecurityAttributes;
MutexHandle: THandle;
begin
InitializeSecurityDescriptor(@SecurityDesc, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(@SecurityDesc, True, nil, False);
SecurityAttr.nLength := SizeOf(SecurityAttr);
SecurityAttr.lpSecurityDescriptor := @SecurityDesc;
SecurityAttr.bInheritHandle := False;
MutexHandle := CreateMutex(@SecurityAttr, False, PChar(MutexName));
if MutexHandle <> 0 then
begin
if GetLastError = ERROR_ALREADY_EXISTS then
begin
MessageBox(0, 'You cannot start more than one instance of ContLab.'
+ #13#10 + 'Use the instance has already started.',
'ContLab', mb_IconHand);
CloseHandle(MutexHandle);
Halt;
end
end;
CreateMutex(@SecurityAttr, False, PChar('Global\' + MutexName));
end;
这样,当用户启动应用程序 2 次时,他会收到一条错误消息,并且第二个实例被终止。
现在我不想显示错误消息,而是打开应用程序第一个实例的主窗体并终止第二个实例。
有可能吗?
【问题讨论】:
-
是的,当然可以。你真正的问题是什么?
-
@KenWhite,您的评论是对 GabrielF 评论的欺骗 :-) 无论如何,这个问题是关于如何打开第一个实例的主要形式,而链接的问题询问如何检测另一个应用程序是否正在运行.
-
我同意@Lurd。尽管假定的副本包含对此问题的答案,但它们是单独的问题。另一个问题的答案是范围蔓延。它回答了合乎逻辑的下一个问题(这个),但不是特别很好。最好将这两个任务放在单独的问题中。这就是我投票重新提出这个问题的原因。
标签: delphi