Exe instance question
Exe instance question
I have the following situation.
Start test.exe
Start the same program test.exe for the second time
The first instance must be closed and the second must be opened.
Is there a way to do this?
Start test.exe
Start the same program test.exe for the second time
The first instance must be closed and the second must be opened.
Is there a way to do this?
Cor de Visser
Registered PureBasic user
Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
Registered PureBasic user
Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
If I remember right, there was a snipped on the Recource Site that does
that. Have a search there, maybe you find it.
You can also have a look at the source of the Editor. It behaves in a
similar way (if a second instance is opened, the second one get's closed)
Timo
that. Have a search there, maybe you find it.
You can also have a look at the source of the Editor. It behaves in a
similar way (if a second instance is opened, the second one get's closed)
Timo
quidquid Latine dictum sit altum videtur
Thanks Freak
Cor de Visser
Registered PureBasic user
Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
Registered PureBasic user
Author of ChordPlanet
Made with PureBasic
http://www.chordplanet.com
Here an small example
Code: Select all
ERROR_ALREADY_EXISTS = 183
Global WM_ACTIVATEOLDINST
WM_ACTIVATEOLDINST = RegisterWindowMessage_("WM_ACTIVATEOLDINST_PB")
MutexName$ = "mylittletestapp"
Procedure MyWindowCallback(WindowID, Message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
If message = WM_ACTIVATEOLDINST
CloseHandle_(hMutex)
OpenConsole()
PrintN("closing app")
Delay(2000)
End
EndIf
ProcedureReturn Result
EndProcedure
hMutex = CreateMutex_(0, 0, @MutexName$)
If GetLastError_() = ERROR_ALREADY_EXISTS
SendMessage_(#HWND_BROADCAST, WM_ACTIVATEOLDINST, 0, 0)
Delay(100)
EndIf
If OpenWindow(0,100,150,450,200,#PB_Window_SystemMenu,"MyLittleTestApp " + Str(Random(500)))
SetWindowCallback(@MyWindowCallback())
Repeat
ev=WaitWindowEvent()
Until ev=#PB_EventCloseWindow
EndIf
If hMutex <> 0
CloseHandle_(hMutex)
EndIf
End
> Here an small example
Here's an even smaller example:
Here's an even smaller example:

Code: Select all
; These two lines prevent this app from being run more than once.
; You must put these lines at the start of your app's code (obviously).
MutexID=CreateMutex_(0,1,"YourAppName") : MutexError=GetLastError_()
If MutexID=0 Or MutexError<>0 : ReleaseMutex_(MutexID) : CloseHandle_(MutexID) : End : EndIf
;
; Your app's code now goes here, until quitting time, which executes the next line.
;
CloseHandle_(MutexID) : End
Re: Exe instance question
PB wrote:> Here an small example
Here's an even smaller example:
No PB, you did not read the whole question, to fast again.Cor wrote:The first instance must be closed and the second must be opened.
First closed.....second....



Re: Exe instance question
> No PB, you did not read the whole question, to fast again.
> First closed.....second....

> First closed.....second....

Just for curiosity ...
What situation could make it worth to start the same program twice only to close the first one immediately after you started the second one?
Or did I miss the point somehow?
I mean: what can the same program do at its second start what it cannot do at first try?
What situation could make it worth to start the same program twice only to close the first one immediately after you started the second one?
Or did I miss the point somehow?
I mean: what can the same program do at its second start what it cannot do at first try?
--------------
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
Yes, its an Irish Wolfhound.
Height: 107 cm; Weight: 88 kg
Here is a modification of the above code!
It let you do single instance running just like any "good" application!
Run the program, minimize the window. Then run it again!
If all goes well the program won't start again,
but instead message the old instance and restore it and bring it to the foreground!
Only tested on XP Pro, but should behave in a similar way on all windows versions. not entirely sure if W95 behave exactly the same or not, test to be sure!
I'll leave it up to you as a coding exercise to find out how to send a message to the old (first) app with a filepath. (like many editors do)
Shouldn't be hard, the messaging "example" is already here so. Hehe!
It let you do single instance running just like any "good" application!
Run the program, minimize the window. Then run it again!
If all goes well the program won't start again,
but instead message the old instance and restore it and bring it to the foreground!
Only tested on XP Pro, but should behave in a similar way on all windows versions. not entirely sure if W95 behave exactly the same or not, test to be sure!
Code: Select all
ERROR_ALREADY_EXISTS = 183
Global WM_ACTIVATEOLDINST
WM_ACTIVATEOLDINST = RegisterWindowMessage_("WM_ACTIVATEOLDINST_PB")
MutexName$ = "mylittletestapp"
Procedure MyWindowCallback(WindowID, message, wParam, lParam)
Result = #PB_ProcessPureBasicEvents
If message = WM_ACTIVATEOLDINST
ShowWindow_(WindowID,#SW_RESTORE)
SetForegroundWindow_(WindowID)
EndIf
ProcedureReturn Result
EndProcedure
hMutex = CreateMutex_(0, 0, @MutexName$)
If GetLastError_() = ERROR_ALREADY_EXISTS
SendMessage_(#HWND_BROADCAST, WM_ACTIVATEOLDINST, 0, 0)
End
EndIf
hwnd=OpenWindow(0,100,150,450,200,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget,"MyLittleTestApp ")
If hwnd
SetWindowCallback(@MyWindowCallback())
Repeat
ev=WaitWindowEvent()
If showoldwindow
EndIf
Until ev=#PB_EventCloseWindow
EndIf
If hMutex <> 0
CloseHandle_(hMutex)
EndIf
End
Shouldn't be hard, the messaging "example" is already here so. Hehe!
-
- Enthusiast
- Posts: 160
- Joined: Wed May 20, 2020 5:19 pm
- Location: The 3rd planet in the Solar System
- Contact:
Re: Exe instance question
Code: Select all
;--------Prevent app from being run more than once.---------------
*a = CreateSemaphore_(NULL,0,1,GetProgramName())
If *a < > 0 And GetLastError_()= #ERROR_ALREADY_EXISTS
CloseHandle_(*a)
End
EndIf
;---------------------------------------------------------
Mac Studio M1Max, PB 6.12 Arm64 and x64.
Macbook Air M2, PB 6.12 Arm64 and x64.
Windows 10, PB 6.12 x64 and x86.
Macbook Air M2, PB 6.12 Arm64 and x64.
Windows 10, PB 6.12 x64 and x86.
Re: Exe instance question
Using a mutex works if you simply want to exit the new instance, but what if you want to focus the window of the previous one (e.g. if the user clicks on the desktop icon to restore a minimized application)? I found some code around here somewhere using filemappings, but it's seemingly broken on 6.1x. Does anyone have a method for doing this?
Thanks.
Thanks.
Re: Exe instance question
I send the commandline parameters to the running instance using TCPIP before closing the second instance.Quin wrote: Wed Jul 24, 2024 5:09 am Using a mutex works if you simply want to exit the new instance, but what if you want to focus the window of the previous one (e.g. if the user clicks on the desktop icon to restore a minimized application)? I found some code around here somewhere using filemappings, but it's seemingly broken on 6.1x. Does anyone have a method for doing this?
Thanks.
Using TCPIP can also be used by other programs to test for a running instance instead of using a mutex
Jim
Re: Exe instance question
This question has nothing to do with this (very) old thread.
But if you are on windows try this
But if you are on windows try this
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Re: Exe instance question
Simple and concise
For Windows
For Windows
Code: Select all
#MyApp = "Test TEST"
Repeat
stx+1
hwnd = FindWindow_(0,#MyApp)
Until hwnd Or stx > 1000
If hwnd
SendMessage_(hwnd, #WM_SYSCOMMAND, #SC_CLOSE, 0)
EndIf
flags = #PB_Window_SystemMenu| #PB_Window_MaximizeGadget| #PB_Window_MinimizeGadget| #PB_Window_ScreenCentered | #PB_Window_SizeGadget
OpenWindow(0,0,0,400,300,"Test TEST",Flags)
StringGadget(0,10,10,120,30,"First Window")
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Quit = 1
Case #PB_Event_Gadget
Select EventGadget()
Case 0
SetGadgetText(0,"Second Test")
EndSelect
EndSelect
Until Quit = 1
End
Egypt my love