Determine if only one instance is running: Difference between revisions

→‎{{header|Tcl}}: Added code to pick a port number magically
(→‎{{header|Tcl}}: Added code to pick a port number magically)
Line 143:
{{trans|Java}} <br>
{{works with|Tcl|8.6}}
<lang Tcl>setpackage APP_SPECIFIC_PORT_NUMBERrequire 12345Tcl 8.6
try {
# Pick a port number based on the name of the main script executing
socket -server closePort -myaddr localhost $APP_SPECIFIC_PORT_NUMBER
procsocket closePort-server {apply {{chan add portargs} {close $chan}}} -myaddr localhost \
[expr {1024+[zlib crc32 [file normalize $::argv0]]%30000}]
} trap {POSIX EADDRINUSE} {} {
# Generate a nice error message
puts stderr "Application $::argv0 already running?"
exit 1
}</lang>
 
Anonymous user