Classic VB - How can I allow only one instance of my application to run at a time?-VBForums
Results 1 to 3 of 3

Thread: Classic VB - How can I allow only one instance of my application to run at a time?

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Candon City, Ilocos Sur, Phils.
    Posts
    10,816

    Classic VB - How can I allow only one instance of my application to run at a time?

    Method 1: You can read the PrevInstance property of the App object, if the value of this is True then another instance of the application is already running.

    If your program starts with Sub Main, you can use this code to exit the program if another copy is already running:
    VB Code:
    1. 'in sub main...
    2. If App.PrevInstance = True Then
    3.     MsgBox "Already running...."
    4.     Exit Sub
    5. End If
    For forms you need an extra piece of code to also close the form, the following code should be placed in Form_load:
    VB Code:
    1. 'in form_load...
    2. If App.PrevInstance = True Then
    3.     MsgBox "Already running...."
    4.     Unload Me
    5.     Exit Sub
    6. End If

    Method 2: You can use the FindWindow API to search for open windows with your form caption.

    Note that for this to work you must know the exact title to find, so if you change your window title within your program

    (such as adding a file name, like Notepad does) then this will fail. Also, if other running programs have the same caption,

    your program may not run at all.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As
    4.  
    5. String) As Long
    6.  
    7. Public Sub Main()
    8.     Dim m_hWnd As Long
    9.  
    10.     'Change Form1 to your form Caption.
    11.     m_hWnd = FindWindow(vbNullString, "Form1")
    12.     If m_hWnd > 0 Then
    13.         MsgBox "Program " & App.Title & " is already running..."
    14.         Exit Sub
    15.     End If
    16.  
    17.     'Change Form1 to your form name.
    18.     Form1.Show
    19. End Sub
    Last edited by si_the_geek; Aug 21st, 2005 at 09:35 AM. Reason: added explanation & tidied up

  2. #2
    New Member
    Join Date
    Nov 2005
    Posts
    1

    Re: Classic VB - How can I allow only one instance of my application to run at a time?

    vb Code:
    1. 'Module
    2. Public Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As Long) As Long
    3. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    4. Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
    5. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    6. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    7. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    8. Private Declare Function FlashWindowEx Lib "user32.dll" (ByRef pfwi As FLASHWINFO) As Long
    9.  
    10.  
    11. Private Const FLASHW_STOP = 0
    12. Private Const FLASHW_CAPTION = &H1
    13. Private Const FLASHW_TRAY = &H2
    14. Private Const FLASHW_ALL = (FLASHW_CAPTION Or FLASHW_TRAY)
    15. Private Const FLASHW_TIMER = &H4
    16.  
    17. Private Const ERROR_ALREADY_EXISTS = 183&
    18. Private Const SW_RESTORE = 9
    19. Private Type SECURITY_ATTRIBUTES
    20.         nLength As Long
    21.         lpSecurityDescriptor As Long
    22.         bInheritHandle As Long
    23. End Type
    24.  
    25. Private Type FLASHWINFO
    26.     cbSize As Long  
    27.     hwnd As Long
    28.     dwFlags As Long
    29.     uCount As Long  
    30.     dwTimeout As Long  
    31. End Type
    32.  
    33.  
    34. Public Mutex As Long
    35.  
    36. Private Sub Main()
    37.     Dim sa As SECURITY_ATTRIBUTES
    38.     Dim hwnd As Long
    39.     Dim FlashInfo As FLASHWINFO
    40.  
    41.     sa.bInheritHandle = 1
    42.     sa.lpSecurityDescriptor = 0
    43.     sa.nLength = Len(sa)
    44.     Mutex = CreateMutex(sa, 1, App.Title)
    45.     If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then        
    46.         hwnd = GetSetting("AppName", "Section", "Key", 0)    
    47.         ShowWindow hwnd, SW_RESTORE        
    48.         SetForegroundWindow hwnd          
    49.        
    50.         FlashInfo.cbSize = Len(FlashInfo)
    51.         FlashInfo.dwFlags = FLASHW_ALL Or FLASHW_TIMER
    52.         FlashInfo.dwTimeout = 0      
    53.         FlashInfo.hwnd = hwnd
    54.         FlashInfo.uCount = 3        
    55.         FlashWindowEx FlashInfo    
    56.     Else
    57.         FrmMain.Show                        
    58.     End If
    59. End Sub

    vb Code:
    1. 'Form
    2. Private Sub Form_Load()
    3.     SaveSetting "AppName", "Section", "Key", Me.hwnd  
    4. End Sub
    5.  
    6. Private Sub Form_Unload(Cancel As Integer)
    7.     ReleaseMutex Mutex                                  
    8.     CloseHandle Mutex
    9.     DeleteSetting "AppName", "Section", "Key"          
    10. End Sub

  3. #3
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,157

    Re: Classic VB - How can I allow only one instance of my application to run at a time

    Here's another method that uses a locked file instead.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.