Results 1 to 10 of 10

Thread: Multiple instances of an app...

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    Montréal, Québec Canada
    Posts
    202
    Hi,

    We use this application which stays open in the task manager when you close it with the X button
    of the window instead of the exit button from the file menu. Sometimes we find up to 10 instances of the same application opened in the task manager because they were closed with the X button. What I need to
    know is, can I use FindWindow or something to determine if the app is already running? The name of the
    window is "Multilink SRB", I want to close the app as soon as two or more of them are present in the task
    manager, but so far, I only get the handle of the first one???

    Any suggestions?

    Phailak

  2. #2
    Addicted Member
    Join Date
    Feb 2001
    Location
    Upstate NY
    Posts
    210

    Verifying

    i don't fully understand the question , but i think that you can go :

    app.previnstance

    on the form load and i think that will stop multiple copys of it from being loaded
    < o >

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    Montréal, Québec Canada
    Posts
    202
    No I can't do that because it's not my app I'm trying to avoid having more than one instance, but I found a solution, thanx anyway

    Phailak

  4. #4
    Addicted Member
    Join Date
    Feb 2001
    Location
    Upstate NY
    Posts
    210

    Smile Oh

    Now i get it, sorry for the confusion
    < o >

  5. #5
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Phailak: here is how you have only 1 instances of your application running all the time.

    Code:
    '//Put this code under the BAsic Module File
    '//And Set the Main() as your entire program
    '//start up function.
    '//And Assume you Application Main Form is call frmMain.
    
    Option Explicit
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Const SW_SHOWNORMAL = 1
    Private xhwnd As Long
    
    Public Sub Main()
    Dim xAppName As String
    If App.PrevInstance Then
        xAppName = "Multilink SRB"
        xhwnd = FindWindow(0&, xAppName)
        If xhwnd <> 0 Then
            AppActivate xAppName
            xhwnd = ShowWindow(xhwnd, SW_SHOWNORMAL)
            End
        End If
    End If
    
    Load frmMain
    frmMain.Show
    End Sub
    you can download the sample code from my home page WIN32API | pre_instance.zip
    Last edited by Chris; Feb 7th, 2001 at 02:15 AM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2000
    Location
    Montréal, Québec Canada
    Posts
    202
    Thanx Chris

    Phailak

  7. #7
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238
    Well, I'm happy too 'coz my sample code does help you

  8. #8
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Unload Form

    The real solution is stopping the problem to begitm with.

    You should unload all forms loaded before terminating the view from your app.
    in example.
    [code]
    private sub Form_Unload
    call Module1.ExitAll
    End Sub

    'In Module1

    Public Sub ExitAll ()
    For Each Form In Forms
    'You can even create a test if any forms are still visible
    if Form.Visible = False Then
    unload Form
    Else
    End if
    Next
    End Sub

  9. #9
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878

    Unhappy

    The code discussed here (and in many other places) always closes the latest (just started) version of the application, and switches to a previous running copy.

    I need to do the opposite!
    Keep the current application running, but delete the earlier copy.

    Any idea how to do that??


    Thanks, Chris

    And if you ask why?
    The latest copy will have been started with a Right Click in Windows Explorer, and use of the SendTo my application. This will pass in COMMAND() with the names of the files I need to work on. If I kill the current app I lose this list of files, so I need to kill the previous one.

  10. #10
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878

    Smile

    Solved:

    The code:
    <code:>

    Private Sub Form_Load()
    Dim AppName, x
    Dim hWindow As Long

    If App.PrevInstance Then
    AppName = App.Title
    App.Title = "Nothing"
    hWindow = FindWindow(vbNullString, AppName)
    App.Title = AppName
    If hWindow <> 0 Then
    x = PostMessage(hWindow, WM_CLOSE, vbNull, vbNull)
    x = WaitForSingleObject(hWindow, INFINITE)
    End If
    End If
    Command2.Caption = "Close the other"
    End Sub

    And the declarations:

    Private Declare Function WaitForSingleObject Lib "kernel32" _
    (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long

    Private Declare Function FindWindow Lib "user32" _
    Alias "FindWindowA" _
    (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

    Private Declare Function PostMessage Lib "user32" _
    Alias "PostMessageA" _
    (ByVal hwnd As Long, _
    ByVal wMsg As Long, _
    ByVal wParam As Long, _
    ByVal lParam As Long) As Long

    'Constants used by the API functions
    Const WM_CLOSE = &H10
    Const INFINITE = &HFFFFFFFF

    </code:>

    I think thats it.
    It changes the new apps window title, finds another app with the original title and kills it, and then sets this apps title back again.

    Cheers, Chris
    Last edited by JordanChris; Feb 15th, 2001 at 12:06 PM.

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