Can you force a single instance of a PB program?

Windows specific forum
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Can you force a single instance of a PB program?

Post 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.
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

--Kale

Image
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Post 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.
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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
:)
Last edited by Kale on Fri Jun 10, 2005 7:51 pm, edited 1 time in total.
--Kale

Image
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post 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
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Post 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!
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Why do you keep changing your nick all the time ?? :wink:
PB&J Lover
Enthusiast
Enthusiast
Posts: 212
Joined: Fri Apr 22, 2005 2:07 pm
Location: U.S.A.
Contact:

Post 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.
-- DB

Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius — and a lot of courage — to move in the opposite direction.

Albert Einstein
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

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

Post 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
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
jqn
User
User
Posts: 97
Joined: Fri Oct 31, 2003 3:04 pm

Post 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
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Post 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
Post Reply