Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Expertise: Beginner
Aug 6, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Activating the Previous Instance of Your App

There are many times when you may not want to allow more than one instance of your application to be launched. In such cases, your program needs to determine if an instance is already running and, if so, activate and close the previous instance. If no previous instance is detected, the program continues normally. The AnotherInstance function determines if the program is already running. If it is, the previous instance is activated and the function returns True. If no previous instance is running, the function returns False. You should call this function when your program starts, preferably from Sub Main. If it returns True, then the program should terminate:
 'Activates the previous instance 
Function AnotherInstance () 
Dim appTitle$ 
 If App.PrevInstance Then 
appTitle$ = App.Title 
'Get our application name 
App.Title = "~!@#$%^&" 
'Set new instance name to unlikely value 
AppActivate appTitle$ 
'Activate previous instance 
AnotherInstance = True 
AnotherInstance = False 
End If 
End If 
AnotherInstance works by checking the PrevInstance property of the App object. If PrevInstance is not zero, then the program is already running. In this case, the function activates the previous instance using AppActivate. Note that AppActivate activates the application with the specified window caption. To prevent AppActivate from activating the current instance, the Title property must be set to a value that is not likely to be the actual caption. Note that this also means that this technique will not work if your application modifies the window caption
Jonathan Wood
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date