From: Murray Steele Date: 2006-11-07T23:33:24+09:00 Subject: Re: The way to restrict a ruby program to just once occurrence on a windows box? ------=_Part_6994_19067411.1162910000482 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Is it some kind of GUI app? I don't know what toolkit you are using, but I do remember from my days as a wxPython jockey that the wxWidgets toolkit came with a api for doing exactly this: wx.SingleInstanceChecker. I imagine that there's a win32 API that is used to do it, and that should be available to most other toolkits you might be using (e.g. I'm sure Java Swing has something similar). I imagine that such an API should be available even if it isn't a GUI app (perhaps through Win32API) and I imagine that using this would be slightly cleaner than doing your own version. Admittedly this isn't much use as all I'm saying is, "yeah, there is a way to do this" without providing much details... Sorry Muz On 07/11/06, Glenn Smith wrote: > > Never mind, solved it (unless anybody knows a better way). > > It's a windows app, so: > > require 'Win32API' > > SW_HIDE = 0 > WS_OVERLAPPED = 0 > WS_DISABLED = 0x08000000 > WS_MINIMIZED = 0x20000000 > > def already_running > # > # Not sure of any other way to do this. > # Create a window with the title "i4FileProcessorHiddenWindow". > When we come to run this program again, > # if there is already a window called > "i4FileProcessorHiddenWindow" then abort that instance. > # In this way, only one instance of the script should run. > # > > findwindow = Win32API.new('user32','FindWindow', ['p','p'], 'L') > return true if findwindow.call("STATIC", > "i4FileProcessorHiddenWindow") != 0 > > createwindow = Win32API.new('user32', 'CreateWindowEx', ['l', 'p', > 'p', 'l', 'i', 'i', 'i', 'i', 'l', 'l', 'l', 'P'], 'l') > dwStyle = (WS_DISABLED | WS_MINIMIZED) > cw = createwindow.call( WS_OVERLAPPED, 'STATIC', > 'i4FileProcessorHiddenWindow', dwStyle, 0, 0, 0, 0, 0, 0, 0, 0 ) > show = Win32API.new('user32', 'ShowWindow', ['L', 'L'], 'L') > show.call(cw, SW_HIDE) > > return false > end > > > On 07/11/06, Glenn Smith wrote: > > I'm rewriting a vb app to ruby (in about a quarter of the code!). If I > ran > > the same ruby program twice in parallel, the second instance should > exit. > > > > In VB6 I could do this: > > > > If App.PrevInstance Then Exit Sub > > > > > > Any ideas how this is done in ruby? > > > > > > Ta muchly > > > > > > > -- > > All the best > Glenn > Aylesbury, UK > > ------=_Part_6994_19067411.1162910000482--