Restored from previous forum. Originally posted by PB.
If my app creates a MutexID to allow only one instance to run, how can
I then detect if my app is run again, and close the previous instance?
PB - Registered PureBasic Coder
Closing an app by its MutexID?
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Danilo.
PB, thats the wrong way.
Its not good to close the previous instance.
If you detect the right MutexID (app is already running),
you send a command to the previous instance
by using DDE or something like that and close
the second instance after that.
Example:
1. instance:
Editor with 5 opened files
2. instance:
Check for MutexID.
When detected, it send something like
"OPEN filename$" to the first instance,
so the opened Editor opens this file too -
and the others are not closed.
All current work/states are not lost by
using this method.
This is how a good application should
handle this stuff.
Well... the PB Editor should do that too..
Examples for DDE should be available here
and on Paul´s site.
cya,
...Danilo
(registered PureBasic user)
PB, thats the wrong way.
Its not good to close the previous instance.
If you detect the right MutexID (app is already running),
you send a command to the previous instance
by using DDE or something like that and close
the second instance after that.
Example:
1. instance:
Editor with 5 opened files
2. instance:
Check for MutexID.
When detected, it send something like
"OPEN filename$" to the first instance,
so the opened Editor opens this file too -
and the others are not closed.
All current work/states are not lost by
using this method.
This is how a good application should
handle this stuff.
Well... the PB Editor should do that too..
Examples for DDE should be available here
and on Paul´s site.
cya,
...Danilo
(registered PureBasic user)
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by PB.
> Its not good to close the previous instance.
My app is a toggle-type: run once to start, run again to close.
So I have to close the previous instance, rather than just stopping
the second one opening.
> If you detect the right MutexID (app is already running),
> you send a command to the previous instance
> by using DDE or something like that and close
> the second instance after that.
Okay, I see what you're saying... so does DDE let me "talk" to the
first instance by using its MutexID alone? Because the MutexID is
the only way I can reference the first instance...
PB - Registered PureBasic Coder
> Its not good to close the previous instance.
My app is a toggle-type: run once to start, run again to close.
So I have to close the previous instance, rather than just stopping
the second one opening.
> If you detect the right MutexID (app is already running),
> you send a command to the previous instance
> by using DDE or something like that and close
> the second instance after that.
Okay, I see what you're saying... so does DDE let me "talk" to the
first instance by using its MutexID alone? Because the MutexID is
the only way I can reference the first instance...
PB - Registered PureBasic Coder
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Berikco.
PB,
You can close the first instance with sendmessage.
Benny
http://www.benny.zeb.be
PB,
You can close the first instance with sendmessage.
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
http://www.benny.zeb.be
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm