Page 1 of 1

Mutex code

Posted: Wed Aug 23, 2006 9:09 pm
by Character
Hi dear experts,

A while ago I grabbed this code from the PureBasic forums to avoid multiple startup of my written programs.
The question is if this code is still sufficient or maybe outdated.
It is doing its task but.. maybe there are better ways?

Example:

Code: Select all

;- Mutex Creation
MutexID=CreateMutex_(0,1,"My Program")
MutexError=GetLastError_()
If MutexID=0 Or MutexError<>0
  ReleaseMutex_(MutexID)
  CloseHandle_(MutexID)
  End
EndIf


If OpenWindow(0,300,300,200,100,"My Program")
  While WaitWindowEvent() <> #PB_Event_CloseWindow : Wend
EndIf
 

;- Mutex Close
CloseHandle_(MutexID)
End

Posted: Wed Aug 23, 2006 9:59 pm
by Num3
It's has good has it get's :D
Pure winapi ...

Damn i love PB !

Re: Mutex code

Posted: Thu Aug 24, 2006 2:05 am
by NoahPhense
No.. that's all you need. I use it in ALL of my apps that I do not want
multiple instancaes of ..

- np

OK

Posted: Thu Aug 24, 2006 8:10 am
by Character
OK thanks!

Posted: Thu Aug 24, 2006 2:20 pm
by TerryHough
Here is a simpler way, again using the WinAPI.

Assume your program's window is named "Calculator".

Code: Select all

; Prevent this program from being launched if it is already running. 
Pgm$="Calculator"               ; Title of this program's window 
a=FindWindow_(0,Pgm$)           ; Is a window with this title is already running"    
If a<>0                           ; Yes, it is already running.
                                    ; Optionally, restore it to it's orginal position and size. 
  If IsZoomed_(a)=#False            ; Is it minimized?. 
    ShowWindow_(a,#SW_RESTORE)      ;   Yes, then restore it. 
  EndIf 
  End                               ; End the attempt to launch this window 
EndIf 

Or, one line of code if you don't care about restoring the already running instance to visibility.

Code: Select all

  
If FindWindow_(0,"Calculator") : End : EndIf
Test this by launching Window's Calculator program and minimizing it. Then run the example code above.

Posted: Thu Aug 24, 2006 7:21 pm
by Character
Here is a simpler way, again using the WinAPI.

Code: Select all

If FindWindow_(0,"Calculator") : End : EndIf 
That looks indeed very simple.
I have red the helpfile about mutex but I don't understand it completely.
Looks like it's used in threads too but for now this is way over my level of knowledge. :?

Is this FindWindow_() way as bulletproof as the mutex one?

Posted: Thu Aug 24, 2006 7:30 pm
by netmaestro

Posted: Thu Aug 24, 2006 7:32 pm
by Xombie
As long as the title of your window never changes, you're probably fine. But if your program sets a new title (like my current IE windows says 'PureBasic :: Post a reply - Microsoft Internet Explorer') then it won't locate the window.

For the mutex solution, you're creating a user-defined name that won't change until you change it. You're testing against an existing mutex object in that case and failing out if it already exists. While the FindWindow_() is basically doing the same, I'd say you're more likely to change the window title than to ever change the mutex name.

..: Edit :.. netmaestro beat me to it ~_~ With much better detail. Personal Note #5082 - Always do research when talking about something with less than 5% complete knowledge :roll:

Posted: Thu Aug 24, 2006 9:14 pm
by PB
> As long as the title of your window never changes, you're probably fine.

But don't forget 3 things that can change a window title:

(1) Different language versions of Windows.
(2) The user running an app that changes it (eg. time on every window).
(3) The window might show "(Not Responding)" during heavy CPU use.

So, go for the mutex method to be 100% safe.

Posted: Thu Aug 24, 2006 9:22 pm
by Character
When you have to drive out some considerable distance because your best client can't get the app to start on his machine, you want to make sure that (a) this never happens to him again and (b) it certainly will never happen to any of his customers! It took a long time to find this one.
I am shocked. Thanks for pointing that one out. I'll stick to the old mutex. That's for sure!


:arrow: TerryHough: thanks anyway for the spontanious tips!
  • I guess nobody stops learning here..