Handling Multiple Instances of Your Application

Most Windows programs are started when users double-click a shortcut icon or menu. If users have 10 programs open, they can switch among them by using the taskbar or pressing Alt+Tab. However, many users like to double-click the shortcut icon again, even if the program is already open. Some shortcuts, such as the My Computer icon in Windows 95, simply bring up the existing window, whereas others launch a new instance of the program. For example, you can open multiple instances of Microsoft Word by selecting it from the Start menu, but double-clicking an associated document’s icon uses an existing copy of Word. To further complicate matters, the Microsoft Office toolbar checks for an active copy of Word and shows it before launching a new one.

If you provide a desktop or toolbar shortcut to your application, users may expect the shortcut to activate an existing application rather than start a new one. This is especially true if users fill out a form and then minimize it to the taskbar. They may click the shortcut expecting the form to reappear, but if you have not coded for this occurrence, another instance of your application is launched, and the users see an empty form.

For certain applications, allowing multiple instances running at the same time may be desirable. However, if you do not design this capability into your application, errors will surely occur. For example, multiple copies of your application might try to write to the same database at the same time. Fortunately, preventing users from accidentally launching extra copies of your application is easy. The following lines of code in the Form_Load event or Sub Main do the job:

If App.PrevInstance = True Then
    MsgBox "Application already running!"
    End
End If

The preceding lines of code check the PrevInstance property of the App object and end the program if another instance is already running. Although this code prevents conflicts and errors, it does not help the users because they still have to find the other application window manually. With a few additional API calls, you can have your program show the previous application before exiting. Listing 18.2 shows a simple routine to display the previous instance before exiting.

Listing 18.2 - Handling Multiple Application Instances

Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
                         (ByVal lpClassName As String, ByVal lpWindowName _
                         As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
                         ByVal nCmdShow As Long) As Long
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) _ As Long 
Private Const SW_RESTORE="9" 
Private Sub Form_Load() Dim sTitle As String Dim hwnd As Long Dim lRetVal As Long 
							If App.PrevInstance Then
								sTitle="Me.Caption" 
								App.Title="newcopy" 
								Me.Caption="newcopy" 
								hwnd="FindWindow(vbNullString," sTitle) 
								If hwnd <> 0 Then 
									lRetVal="ShowWindow(hwnd," SW_RESTORE) 
									lRetVal="SetForegroundWindow(hwnd)" 
								End If 
							End 
							End If 
			End Sub 

In the preceding code, you first rename the current application’s title and form caption properties so that the FindWindow API function cannot find it. Then you use three API calls to find the window belonging to the previous application, restore it, and move it to the foreground.

The methods for dealing with multiple instances described here
pertain to a Standard EXE project.

Top Home