About   -   Contact   -   Purchase   -   Search   -   What's New

Run an Application Only Once
This tip was submitted by Loganathan Chettiar.

Only one instance of any application running at a time is good to make it simple and sleek. In this discussion we are going to see how to make your application run only once.

When user double clicks on your application icon, 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:

1. Create a non-visual user object 'n_cst_win32sdk' with following local functions and external functions.

FUNCTION boolean ShowWindow( ulong winhandle,&
   int wincommand ) Library "user32"
FUNCTION boolean BringWindowToTop(  ulong  HWND  )&
   Library "user32"
FUNCTION long FindWindowA( ulong  Winhandle, &
   string wintitle ) Library "user32"

Local  :
Of_FindWindowByTitle( string as_wintitle )
        Return FindWindowA( 0, as_wintitle )

Of_ShowWindow( unsignedlong     al_winhandle )
        Return ShowWindow( al_winhandle, 5 )

Of_BringToTop( unsignedlong al_winhandle )
        Return BringWindowToTop( al_winhandle )

2. Create a small test window 'w_test' with one close button to close the window. Give window the title "First Window".

3. In the application Open event code the following:

n_cst_win32sdk  lnv_sdk
long            ll_winhandle

If Not IsValid( lnv_sdk ) Then
   lnv_sdk     =      Create n_cst_win32sdk
End if  
ll_winhandle = &
   lnv_sdk.of_FindWindowByTitle( "First Window" )
If ll_winhandle > 0 Then
   lnv_sdk.of_BringToTop( ll_winhandle )
   lnv_sdk.of_ShowWindow( ll_winhandle )
   destroy lnv_sdk
End If
Open( w_sdktest )

If the window is found, it means it is already running, so bring the window to top and return. The Source code to this user object can be found on the software page.


Top of Page

Legal Notice

Ken Howe 2011