Results 1 to 5 of 5

Thread: PrevInstance kills FIRST instance

  1. #1

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591

    PrevInstance kills FIRST instance

    I want just one instance of my app running at a time. When a second instance of my app occurs, I need to have the app start from scratch. I can't just set focus to the first instance and kill the second instance.


    My app is only run when called from another program. Certain events in that other program cause it to set some data and then run my app. My app then gets the data and does its thing. If the user doesn't close my app when they are done, the other program will keep running new instances. If a second instances occurs, I want to kill one instance. But, whichever instance of my app that I keep running needs to go get the new data. So, if there is a PrevInstance, I need to either:

    1) Kill the first instance and let the second instance run and load the new data.

    or

    2) Kill the second instance and somehow get the first instance to load the new data. I can't find any Form events (Initialize, Load, Activate, GotFocus, etc.) that I can cause to happen from the second event (especially considering that the new data shuld only be loaded by the program and never the user. So I'm thinking I should go with option 1, but I'm not sure how to kill the first instance of the app?

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429
    G'day WorkHorse,

    This is a simplistic approach (I modified this previous post of Marty's):
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    4. Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    5. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    6.  
    7. Const GW_HWNDPREV = 3
    8. Const WM_CLOSE = &H10
    9. Const WM_QUIT = &H12
    10.  
    11. Private Sub Form_Load()
    12.  
    13.     If App.PrevInstance Then
    14.         TerminatePrevInstance
    15.     End If
    16.  
    17. End Sub
    18.  
    19. Sub TerminatePrevInstance()
    20. Dim sOldTitle As String
    21. Dim lPrevHndl As Long
    22.  
    23.     'Save the title of the application.
    24.     sOldTitle = App.Title
    25.  
    26.     'Rename the title of this application so FindWindow
    27.     'will not find this application instance.
    28.     App.Title = "unwanted instance"
    29.  
    30.     'Attempt to get window handle using VB4 class name.
    31.     lPrevHndl = FindWindow("ThunderRTMain", sOldTitle)
    32.  
    33.     'Check for no success.
    34.     If lPrevHndl = 0 Then
    35.         'Attempt to get window handle using VB5 class name.
    36.         lPrevHndl = FindWindow("ThunderRT5Main", sOldTitle)
    37.     End If
    38.  
    39.     'Check if found
    40.     If lPrevHndl = 0 Then
    41.         'Attempt to get window handle using VB6 class name
    42.         lPrevHndl = FindWindow("ThunderRT6Main", sOldTitle)
    43.     End If
    44.  
    45.     'Check if found
    46.     If lPrevHndl = 0 Then
    47.         'No previous instance found, restore App Title
    48.         App.Title = sOldTitle
    49.         Exit Sub
    50.     End If
    51.  
    52.     'Get handle to previous window.
    53.     lPrevHndl = GetWindow(lPrevHndl, GW_HWNDPREV)
    54.  
    55.     PostMessage lPrevHndl, WM_CLOSE, 0, 0
    56.     PostMessage lPrevHndl, WM_QUIT, 0, 0
    57.  
    58. End Sub

    I havn't tested it




    Bruce.
    Last edited by Bruce Fox; Feb 3rd, 2004 at 09:11 PM.

  3. #3

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Thanks. That will do the trick. I expected I'd have to do something like that. I don't see any reason to not restore the app title whether or not the hWnd of the previous instance has been found, so I adjusted that to keep the proper app name in task manager. Works great.

  4. #4

  5. #5

    Thread Starter
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Originally posted by MartinLiss
    Glad I could help

Posting Permissions

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



Click Here to Expand Forum to Full Width