如何判断我的程序的一个实例是否正在运行?我以为我可以用数据文件做这个但它只是凌乱:(
我想这样做,因为我只希望一个实例在一个点上打开.
我知道这是一个坏主意的所有原因.如果一个应用程序窃取输入焦点我不喜欢它,但这纯粹是个人使用,我希望它发生; 它不会打扰任何东西.
(对于好奇:我在NetBeans中运行单元测试,它生成一个日志文件.当我的后台应用程序看到日志文件的时间戳更改时,我希望它解析日志文件并转到前面显示结果).
这个问题没有帮助,也没有谷歌搜索.似乎BringToFront()长时间没有工作,我找不到任何替代方案.
有任何想法吗?
我用Delphi XE3创建了一个应用程序.我的应用程序有一个trayicon(我使用TCoolTrayIcon),所以当用户最小化它时,任务栏上没有图标,只有在trayicon上.
为了避免更多的应用程序,我使用此代码:
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\' + …Run Code Online (Sandbox Code Playgroud)