Determine if only one instance is running: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 222:
return 0;
}</lang>
 
=={{header|C++}}==
===Microsoft Windows===
{{works with|Windows|2000 or later}}
This line needs to be near the top of the file (or in stdafx.h, if you use one.)
<lang cpp>#include <afx.h></lang>
 
You need a variable of type HANDLE with the same lifetime as your program. Perhaps as a member of your CWinApp object.
<lang cpp>HANDLE mutex;</lang>
 
At the earliest possible point in your program, you need to initialize it and perform your check. "MyApp" should be a string unique to your application. See [http://msdn2.microsoft.com/en-us/library/ms682411.aspx here] for full details.
 
<lang cpp>mutex = CreateMutex( NULL, TRUE, "MyApp" );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
// There's another instance running. What do you do?
}</lang>
 
Finally, near the end of your program, you need to close the mutex.
<lang cpp>CloseHandle( mutex );</lang>
 
=={{header|C sharp|C#}}==
Line 345 ⟶ 325:
}
}</lang>
 
=={{header|C++}}==
===Microsoft Windows===
{{works with|Windows|2000 or later}}
This line needs to be near the top of the file (or in stdafx.h, if you use one.)
<lang cpp>#include <afx.h></lang>
 
You need a variable of type HANDLE with the same lifetime as your program. Perhaps as a member of your CWinApp object.
<lang cpp>HANDLE mutex;</lang>
 
At the earliest possible point in your program, you need to initialize it and perform your check. "MyApp" should be a string unique to your application. See [http://msdn2.microsoft.com/en-us/library/ms682411.aspx here] for full details.
 
<lang cpp>mutex = CreateMutex( NULL, TRUE, "MyApp" );
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
// There's another instance running. What do you do?
}</lang>
 
Finally, near the end of your program, you need to close the mutex.
<lang cpp>CloseHandle( mutex );</lang>
 
=={{header|Clojure}}==
Line 353:
(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|D}}==
===Unix Domain Socket===
Line 412 ⟶ 385:
}
</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|Erlang}}==
Line 678 ⟶ 680:
canopen()
</lang>
 
 
=={{header|Kotlin}}==
Line 796 ⟶ 797:
}
</lang>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 921:
 
sleep 60; # then your code goes here</lang>
=={{header|Perl 6}}==
{{works with|rakudo|2018.03}}
An old-school Unix solution, none the worse for the wear:
<lang perl6>my $name = $*PROGRAM-NAME;
my $pid = $*PID;
 
my $lockdir = "/tmp";
my $lockfile = "$lockdir/$name.pid";
my $lockpid = "$lockfile$pid";
my $havelock = False;
 
END {
unlink $lockfile if $havelock;
try unlink $lockpid;
}
 
my $pidfile = open "$lockpid", :w orelse .die;
$pidfile.say($pid);
$pidfile.close;
 
if try link($lockpid, $lockfile) {
$havelock = True;
}
else {
shell "kill -CONT `cat $lockfile` || rm $lockfile";
if try link($lockfile, $lockpid) {
$havelock = True;
}
else {
die "You can't run right now!";
}
}
note "Got lock!";
unlink $lockpid;</lang>
 
=={{header|Phix}}==
Line 1,082 ⟶ 1,048:
(sleep 10)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2018.03}}
An old-school Unix solution, none the worse for the wear:
<lang perl6>my $name = $*PROGRAM-NAME;
my $pid = $*PID;
 
my $lockdir = "/tmp";
my $lockfile = "$lockdir/$name.pid";
my $lockpid = "$lockfile$pid";
my $havelock = False;
 
END {
unlink $lockfile if $havelock;
try unlink $lockpid;
}
 
my $pidfile = open "$lockpid", :w orelse .die;
$pidfile.say($pid);
$pidfile.close;
 
if try link($lockpid, $lockfile) {
$havelock = True;
}
else {
shell "kill -CONT `cat $lockfile` || rm $lockfile";
if try link($lockfile, $lockpid) {
$havelock = True;
}
else {
die "You can't run right now!";
}
}
note "Got lock!";
unlink $lockpid;</lang>
 
=={{header|REXX}}==
Line 1,209 ⟶ 1,211:
 
}</lang>
 
=={{header|Sidef}}==
<lang ruby># For this to work, you need to explicitly
10,327

edits