Determine if only one instance is running: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 222: | Line 222: | ||
return 0; |
return 0; |
||
}</lang> |
}</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#}}== |
=={{header|C sharp|C#}}== |
||
Line 345: | Line 325: | ||
} |
} |
||
}</lang> |
}</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}}== |
=={{header|Clojure}}== |
||
Line 353: | Line 353: | ||
(try (new ServerSocket *port* 10 (. InetAddress getLocalHost)) |
(try (new ServerSocket *port* 10 (. InetAddress getLocalHost)) |
||
(catch IOException e (System/exit 0))) ; port taken, so app is already running </lang> |
(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}}== |
=={{header|D}}== |
||
===Unix Domain Socket=== |
===Unix Domain Socket=== |
||
Line 412: | Line 385: | ||
} |
} |
||
</lang> |
</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}}== |
=={{header|Erlang}}== |
||
Line 678: | Line 680: | ||
canopen() |
canopen() |
||
</lang> |
</lang> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 796: | Line 797: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
Line 921: | Line 921: | ||
sleep 60; # then your code goes here</lang> |
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}}== |
=={{header|Phix}}== |
||
Line 1,082: | Line 1,048: | ||
(sleep 10) |
(sleep 10) |
||
</lang> |
</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}}== |
=={{header|REXX}}== |
||
Line 1,209: | Line 1,211: | ||
}</lang> |
}</lang> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<lang ruby># For this to work, you need to explicitly |
<lang ruby># For this to work, you need to explicitly |