This code has been declared by its author to be Public Domain code.
Download source code | Prevent multiple process by JoshK | 2010 |
| |
|
Just import this file into an event-driven app. You don't need to do anything else. |
SuperStrict
Import bah.interprocess
Private
OnEnd cleanup
Function Cleanup()
Local applock:TApplock
For applock=EachIn TApplock.map.values()
applock.Free()
Next
EndFunction
Public
New TApplock
Type TAppLock
Const TIMEOUT:Int = 1000*20
Global map:TMap=New TMap
Field shareddata:TBank
Field timer:TTimer
Method New()
shareddata=CreateSharedBank("AppLockData_"+StripDir(AppFile),4)
If MilliSecs()-shareddata.PeekInt(0)<TIMEOUT
Notify "Another copy of "+StripDir(AppFile)+" is already running. You may only run one copy of this program.",1
End
EndIf
Update()
timer=CreateTimer(1000.0/(TIMEOUT/2.0))
map.insert(timer,Self)
AddHook EmitEventHook,EventHook,Self
EndMethod
Method Delete()
Free()
EndMethod
Method Update()
shareddata.PokeInt(0,MilliSecs())
EndMethod
Function EventHook:Object(id:Int,data:Object,context:Object)
Local applock:TApplock
Local event:TEvent=TEvent(data)
If event
If event.id = EVENT_TIMERTICK
applock = TAppLock(map.valueforkey(event.source))
If applock applock.Update()
EndIf
EndIf
Return data
EndFunction
Method Free()
shareddata.PokeInt(0,0)
map.remove(timer)
timer.stop()
EndMethod
EndType |
Comments |
This works well, but if you have your application running and you sign in as a different user on your computer; You get the following error when you run it:
This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.
|
|
PS: Changing the CreateSharedBank() call to include the username works.
|
Code Archives Forum