Can you force a single instance of a PB program?
- 
				PB&J Lover
 - 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?
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.
			
			
									
									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
						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&J Lover
 - Enthusiast

 - Posts: 212
 - Joined: Fri Apr 22, 2005 2:07 pm
 - Location: U.S.A.
 - Contact:
 
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.
									
			
									
						- NoahPhense
 - Addict

 - Posts: 1999
 - Joined: Thu Oct 16, 2003 8:30 pm
 - Location: North Florida
 
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
			
			
									
									
						www.liquidbuzz.com ..
The source is VERY simple, I can post it when I get home from work.
- np
- 
				PB&J Lover
 - Enthusiast

 - Posts: 212
 - Joined: Fri Apr 22, 2005 2:07 pm
 - Location: U.S.A.
 - Contact:
 
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:
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!
			
			
									
									Code: Select all
PVGadgets_StartOnce("myprogram.exe")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
						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
- utopiomania
 - Addict

 - Posts: 1655
 - Joined: Tue May 10, 2005 10:00 pm
 - Location: Norway
 
- 
				PB&J Lover
 - Enthusiast

 - Posts: 212
 - Joined: Fri Apr 22, 2005 2:07 pm
 - Location: U.S.A.
 - Contact:
 
Re: Can you force a single instance of a PB program?
> Can I force a PB program to only launch a single instance of itself?
It's in the FAQ: viewtopic.php?t=4876
			
			
									
									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.
						"PureBasic won't be object oriented, period" - Fred.
Use MUTEX:
JOAQUIN
			
			
									
									
						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
- NoahPhense
 - Addict

 - Posts: 1999
 - Joined: Thu Oct 16, 2003 8:30 pm
 - Location: North Florida
 
Compile this with TailBite, you'll have your own lib.
Make this your the first part of your code:
lbdt_Mutex_Open(name.s)
Then, just before END.. use: lbdt_Mutex_Close()
- np
			
			
									
									
						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)
EndProcedurelbdt_Mutex_Open(name.s)
Then, just before END.. use: lbdt_Mutex_Close()
- np


