Page 1 of 1

Can you force a single instance of a PB program?

Posted: Fri Jun 10, 2005 5:48 pm
by PB&J Lover
Hello PB users,

Can I force a PB program to only launch a single instance of itself?

In other words, if the user keeps launching the program, I don't get more than one copy running at a time.

How do you do that?

Thanks.

Posted: Fri Jun 10, 2005 5:56 pm
by Kale

Posted: Fri Jun 10, 2005 6:17 pm
by PB&J Lover
Thanks, but it appears the link to the library is no longer valid. I'll see if I can track it down.

Posted: Fri Jun 10, 2005 6:34 pm
by Kale
You could use this example created from an old source i have, dunno where it come from?

Code: Select all

;===========================================================================
;-CONSTANTS
;===========================================================================

#APP_NAME = "Testing one instance"

Enumeration
	#WINDOW_ROOT
EndEnumeration

;===========================================================================
;-PROCEDURES
;===========================================================================

;Handle an error
Procedure HandleError(Result, Text.s)
	If Result = 0 : MessageRequester("Error", Text, #PB_MessageRequester_Ok) : End : EndIf
EndProcedure

;start only one program
Procedure OnlyOneProgramStart(Name.s)
	Shared OnlyOneStartMutex.l
	OnlyOneStartMutex = CreateMutex_(0, 1, Name)
	OnlyOneStartError.l = GetLastError_()
	If OnlyOneStartMutex <> 0 And OnlyOneStartError = 0
		ProcedureReturn 1
	Else
		CloseHandle_(OnlyOneStartMutex)
		End
	EndIf
EndProcedure

;close the program mutex
Procedure OnlyOneProgramStop()
	CloseHandle_(OnlyOneStartMutex)
EndProcedure

;===========================================================================
;-GEOMETRY
;===========================================================================

HandleError(OpenWindow(#WINDOW_ROOT, 0, 0, 400, 300, #PB_Window_SystemMenu | #PB_Window_ScreenCentered, #APP_NAME), "Main window could not be created.")

;===========================================================================
;-MAIN LOOP
;===========================================================================

OnlyOneProgramStart(#APP_NAME)

Repeat
	EventID.l = WaitWindowEvent()
		
Until EventID = #PB_Event_CloseWindow

OnlyOneProgramStop()

End
:)

Posted: Fri Jun 10, 2005 7:43 pm
by NoahPhense
Or you can use my lib.. found here

www.liquidbuzz.com ..

The source is VERY simple, I can post it when I get home from work.

- np

Posted: Fri Jun 10, 2005 7:57 pm
by PB&J Lover
I tracked down the source from that thread (it was the guys at Pure Vision). Since I am a PV user ("PV" stands for Pure Vision and should not be confused with drugs like PCP which has an extra "P" and a "C" instead of a "V") I get to use their:

Code: Select all

PVGadgets_StartOnce("myprogram.exe")
This works very well. What I'd like now is something that will bring up an app from the taskbar that has been minimized when another instance is evoked.

Thanks all!

Posted: Fri Jun 10, 2005 9:34 pm
by utopiomania
Why do you keep changing your nick all the time ?? :wink:

Posted: Fri Jun 10, 2005 9:40 pm
by PB&J Lover
I didn't. I asked them to change it and they did during this thread. Just this once. I like it.

Re: Can you force a single instance of a PB program?

Posted: Sat Jun 11, 2005 2:27 am
by PB
> Can I force a PB program to only launch a single instance of itself?

It's in the FAQ: viewtopic.php?t=4876

Posted: Sat Jun 11, 2005 9:51 am
by jqn
Use MUTEX:

Code: Select all

  MutexID=CreateMutex_(0,1,"MUTEX MyProgram")
  MutexError=GetLastError_()
  If MutexID=0 Or MutexError<>0
    MessageRequester("Error, "Program already running", #PB_MessageRequester_Ok)
    ReleaseMutex_(MutexID)
    CloseHandle_(MutexID)
    End
  EndIf
JOAQUIN

Posted: Sat Jun 11, 2005 3:09 pm
by NoahPhense
Compile this with TailBite, you'll have your own lib.

Code: Select all

Global MutexID.l
Global MutexError.l

ProcedureDLL.l lbdt_Mutex_Open(name.s); any string you want
MutexID = CreateMutex_(0, 1, name)
MutexError = GetLastError_()
If MutexID = 0 Or MutexError <> 0
  ReleaseMutex_(MutexID)
  CloseHandle_(MutexID)
  End
EndIf
EndProcedure

ProcedureDLL.l lbdt_Mutex_Close(); thats all
ReleaseMutex_(MutexID)
CloseHandle_(MutexID)
EndProcedure
Make this your the first part of your code:
lbdt_Mutex_Open(name.s)



Then, just before END.. use: lbdt_Mutex_Close()

- np