Created
March 27, 2012 15:06
-
-
Save nrk/2216699 to your computer and use it in GitHub Desktop.
Single instance check with ruby and windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'win32/mutex' | |
require 'windows/error' | |
def with_global_lock(lockname, &block) | |
mutex = Win32::Mutex.new(true, lockname) | |
if mutex.GetLastError == Windows::Error::ERROR_ALREADY_EXISTS | |
raise RuntimeError, 'Already running!' | |
end | |
begin | |
block.call | |
ensure | |
mutex.release | |
mutex.close | |
end | |
end | |
with_global_lock('my_unique_lock_name') do | |
10.times do |i| | |
puts i | |
sleep 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment