Determine if only one instance is running: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: IupCloseOnEscape no longer needed)
(Added Wren)
Line 1,358: Line 1,358:
<lang vb>Dim onlyInstance as Boolean
<lang vb>Dim onlyInstance as Boolean
onlyInstance = not App.PrevInstance</lang>
onlyInstance = not App.PrevInstance</lang>

=={{header|Wren}}==
This works by trying to create a special file with exclusive access.

If this fails then the error is captured, a suitable message is printed and the script exits.

Otherwise, the script completes normally and the special file is deleted just before it exits.
<lang ecmascript>import "io" for File, FileFlags

var specialFile = "wren-exclusive._sp"
var checkOneInstanceRunning = Fn.new {
// attempt to create the special file with exclusive access
var ff = FileFlags.create | FileFlags.exclusive
File.openWithFlags(specialFile, ff) { |file| } // closes automatically if successful
}

// check if the current instance is the only one running
var fiber = Fiber.new {
checkOneInstanceRunning.call()
}
var error = fiber.try()
if (error) {
System.print("An instance is already running.")
return
}

// do something that takes a while for testing purposes
var sum = 0
for (i in 1...1e8) {
sum = sum + i
}
System.print(sum)

File.delete(specialFile) // clean up</lang>

{{out}}
<pre>
(in window 1)
$ wren one_instance_running.wren
4.99999995e+15
$

(in window2, started before the first instance completes)
$ wren one_instance_running.wren
An instance is already running.
$
</pre>


{{omit from|ACL2}}
{{omit from|ACL2}}