Reputation: 7388
I want to allow only one instance of my app to run. I'm using the code below to close additional instances of my app, but the problem is it will kill the current session as well. How can i edit this code to close additional instances, but not the current one ?
function killDuplicates: integer;
const
PROCESS_TERMINATE = $0001;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
pna : string;
begin
Result := 0;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
while Integer(ContinueLoop) <> 0 do
begin
pna := lowercase(ExtractFileName(FProcessEntry32.szExeFile));
if pna = 'myapp.exe' then
Result := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE, BOOL(0), FProcessEntry32.th32ProcessID), 0));
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
Upvotes: 2
Views: 1296
Reputation: 21252
To answer the question asked:
How can i edit this code to close additional instances, but not the current one ?
Compare FProcessEntry32.th32ProcessID
with GetCurrentProcessId
.
if they match, it's your current process.
That said, I strongly urge you not to use this kind of method to create a single instance application, as already mentioned by @Jerry.
consider this: Your program is running and the user interacts with it already. Now the user launches a second instance. What do you think will happen? The already running application will be killed literally by the second instance. Instead let the second instance terminate itself gracefully if it finds an already running instance (optionally notifying the running process about it, so it can bring itself to the front for example).
Take a look here for possible solutions:
How to ensure only a single instance of my application runs?
How can I tell if another instance of my program is already running?
Upvotes: 8