devxlogo

Activating a Previous Instance of an Application

Activating a Previous Instance of an Application

To prevent users from launching multiple instances of your application,check the PrevInstance property of VB’s App object; if the propertyis True, the program is already running. To activate the previousinstance, call Windows’ FindWindow, ShowWindow, and SetFocus APIs:

 Global Const SW_RESTORE=9Declare Function FindWindow Lib "User" _         (ByVal lpClassName As Any, ByVal _        lbWindowName As Any) As IntegerDeclare Function ShowWindow Lib "User" _         (ByVal hWnd As Integer, ByVal _        nCmdShow As Integer) As IntegerDeclare Function SetFocusAPI Lib _        "User" Alias "SetFocus" (ByVal _        hWnd As Integer) As IntegerSub Main ()        Dim hWnd As Integer        Dim iResult As Integer        ' App object's Title property may be         ' set at runtime, as illustrated         ' here, or at compile time in VB's         ' Make .EXE' dialog.        App.Title = "Test Application"        If App.PrevInstance Then                ' ThunderForm is the class name                 ' for VB forms (Thunder was the                 ' original code name for Visual                 ' Basic within 'Microsoft)                ' Find the existing instance                hWnd = FindWindow("Thunder_                        Form", App.Title)                ' FindWindow function returns a                 ' non-zero value if it finds a                 matching window                 If hWnd <> 0 Then                        ' Restore window if minimized                        iResult = ShowWindow(hWnd,                                SW_RESTORE)' Set focus to the specified window                        iResult = SetFocusAPI(hWnd)                        ' Close this instance                        End                End If        End If        ' If no previous instance, show        ' main form and proceed normally.        frmMain.ShowEnd Sub
See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist