[ Trick] single instance runnable

Share your advanced PureBasic knowledge/code with the community.
User avatar
microdevweb
Enthusiast
Enthusiast
Posts: 179
Joined: Fri Jun 13, 2014 9:38 am
Location: Belgique

[ Trick] single instance runnable

Post by microdevweb »

Hello all,

if you want of users cannot be run more one instance of your executable, see this code below (only for windows)

Code: Select all

#MyApp="Your message"
Mutex=CreateMutex_(0,1,#MyApp)
If GetLastError_()=#ERROR_ALREADY_EXISTS
  MessageRequester(#MyApp,"one instance is already running",#PB_MessageRequester_Error)
  End
EndIf

; your main code

#MAIN_FORM = 0
OpenWindow(#MAIN_FORM,0,0,800,600,"Hello world",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)

Repeat
  WaitWindowEvent()
Until Event() = #PB_Event_CloseWindow 

; don't forget to unlock the mutex
ReleaseMutex_(Mutex)
End

Use Pb 5.73 lst and Windows 10

my mother-language isn't english, in advance excuse my mistakes.