Determine if only one instance is running: Difference between revisions

Added Delphi example
(Added Delphi example)
Line 216:
(try (new ServerSocket *port* 10 (. InetAddress getLocalHost))
(catch IOException e (System/exit 0))) ; port taken, so app is already running </lang>
=={{header|Delphi}}==
<lang Delphi>program OneInstance;
 
{$APPTYPE CONSOLE}
 
uses SysUtils, Windows;
 
var
FMutex: THandle;
begin
FMutex := CreateMutex(nil, True, 'OneInstanceMutex');
if FMutex = 0 then
RaiseLastOSError
else
begin
try
if GetLastError = ERROR_ALREADY_EXISTS then
Writeln('Program already running. Closing...')
else
begin
// do stuff ...
Readln;
end;
finally
CloseHandle(FMutex);
end;
end;
end.</lang>
 
=={{header|Go}}==
Solution using O_CREATE|O_EXCL, and with messages and a 10 second sleep to let you test it.
Anonymous user