Run an Application Only Once
Posted In:
Win32 API
.
By popo
In this tips, I would like to discuss how to make an application run only once at a time. When user try to run your application, first try to see if it is already running. If it is already running bring it to top and return. There are some sleek SDK functions you can make use of to implement this. The following are the steps to create this feature:
- Create a "Custom Class UserObject"
- Set the AutoInstantiate to TRUE
- Declare the Win32 API modul at the UserObject on Declare -> Local External Functions
Function Long GetLastError () Library 'kernel32.dll'
Function ULong CreateMutex (ULong lpsa, Boolean fInitialOwner, String lpszMutexName) Library 'kernel32.dll' Alias for CreateMutexA - Create a UserObject Function like this:
String ls_name
If Handle(GetApplication()) > 0 Then
ls_name = GetApplication().AppName + Char(0)
CreateMutex(0, True, ls_name)
If GetLastError() = 183 Then Return True
End If
Return False - Save the UserObject as uo_mutex (for example)
uo_mutex lou_mutex
If lou_mutex.uf_isrunning() Then
MessageBox ('Warning', 'Application is already running', StopSign!)
HALT CLOSE
End If
//...
// Your next line code
//...