Mutex code

Just starting out? Need help? Post your questions and find answers here.
Character
Enthusiast
Enthusiast
Posts: 337
Joined: Mon Aug 07, 2006 3:51 pm
Location: Netherlands

Mutex code

Post 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
Cessante causa cessat effectus
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

It's has good has it get's :D
Pure winapi ...

Damn i love PB !
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Mutex code

Post 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
Character
Enthusiast
Enthusiast
Posts: 337
Joined: Mon Aug 07, 2006 3:51 pm
Location: Netherlands

OK

Post by Character »

OK thanks!
Cessante causa cessat effectus
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Post 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.
Character
Enthusiast
Enthusiast
Posts: 337
Joined: Mon Aug 07, 2006 3:51 pm
Location: Netherlands

Post 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?
Cessante causa cessat effectus
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

BERESHEIT
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post 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:
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Character
Enthusiast
Enthusiast
Posts: 337
Joined: Mon Aug 07, 2006 3:51 pm
Location: Netherlands

Post 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..
Cessante causa cessat effectus
Post Reply