Results 1 to 18 of 18

Thread: Preventing a second instance application

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    -
    Posts
    101
    How can I prevent the application from being runned twice?

    - i'm using VB 6.0 Enterprise Edition
    icq: 16228887

  2. #2
    Guest
    Code:
    Private Sub Form_Load()
        If App.PrevInstance = True Then End
    End Sub

  3. #3
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    Tut, tut, tut, Megatron. We use:

    Code:
    Unload Me
    Nowadays.

    I forgive you.
    Courgettes.

  4. #4
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    This is discussed more at the following threat

    http://forums.vb-world.net/showthrea...threadid=26409


    Things I do when I am bored: DotNetable

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Location
    -
    Posts
    101
    Thanks Again!!!
    icq: 16228887

  6. #6
    Guest
    Actually, V(ery) Basic, the correct way is:
    Code:
    Dim frm As Form
    
    For Each frm In Forms
        Unload frm
        Set frm = Nothing
    Next

  7. #7
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736

    Question

    If another instance of the application is found, and that application was minimized by the user, is it possible to activate that instance and maximize it ?

    Thanks.

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    I think this will work
    Code:
    If App.PrevInstance Then
            ' Activate the previous instance
            AppActivate App.Title
            
            ' Send a key (here SHIFT-key) to set the
            ' form from the previous instance to the
            ' top of the screen.
            SendKeys "+", True
            
            End If
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Thanks for the reply. That code hi-lites the minimized application, but does not maximize it.

    I'll try some different key combinations and see if anything works.

  10. #10
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    Once the minimized application is hi-lited, manually performing CTRL-M will maximize the window after I click on the app, but when I put those keys in code the window does not maximize.

    I am using: SendKeys ("^M"),True


  11. #11
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    This works for me:[code]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 Const SW_SHOWMAXIMIZED = 3

    Private Sub Form_Load()
    Dim lhwnd As Long
    Dim lRtrn As Long
    Dim X As Form

    If App.PrevInstance = True Then
    'Get Window Handle
    lhwnd = FindWindow(vbNullString, App.Title)

    'Activate and Maximize Window
    lRtrn = ShowWindow(lhwnd, SW_SHOWMAXIMIZED)

    'Kill App
    For Each X In Forms
    Unload X
    Set X = Nothing
    Next
    End If
    End Sub


    [Edited by WadeD on 08-18-2000 at 11:46 AM]
    Wade

  12. #12
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    I'm not sure why, but that code does not maximize the original application for me. It does kill the second instance though.

    Thanks for the code. I'll keep experimenting with it.

  13. #13
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    My mistake...it works if you're doing this for another window like "Untitled - Notepad". If there are multiple instances, it acts on the first window it finds with that caption, which in this case is itself so it stops executing.

    [Edited by WadeD on 08-18-2000 at 12:35 PM]
    Wade

  14. #14
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    What I gave you previously works for a separate app (like Word or Excel).
    This works for killing the second instance of the same app and activating/maximizing the first instance:
    Code:
    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 Const SW_SHOWMAXIMIZED = 3
    
    Private Sub Form_Load()
    Dim lhwnd As Long
    Dim lRtrn As Long
    Dim sFrmCaptn As String
    Dim X As Form
    
        'Get Caption to Search For
        sFrmCaptn = Me.Caption
        Me.Caption = ""
    
        'Search for Window / Get its Handle
        lhwnd = FindWindow(vbNullString, sFrmCaptn)
        
        'If Another Window Found
        If lhwnd <> 0 Then
            'Activate and Maximize Window
            lRtrn = ShowWindow(lhwnd, SW_SHOWMAXIMIZED)
            
            'Kill App
            For Each X In Forms
                Unload X
                Set X = Nothing
            Next
            Unload Me
    
        'If No Other Window Found    
        Else
            'Set Caption back to Original Caption
            Me.Caption = sFrmCaptn
        End If
    End Sub
    Wade

  15. #15
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    I appreciate you taking the time to come up with this code, but it is not working for me. Instead, two instances of the application are open.

    I have my code to the point where it will at least inform the user that the application is already running, which should be good enough for now.

    I'll save the enhancements for later.

    Thanks again.

  16. #16
    Hyperactive Member
    Join Date
    Nov 1999
    Posts
    363
    Hmmm...it's a copy-and-paste directly from a new project in which it works for me. Some API's work differently on different OS's. I'm in Win NT, using VB6 SP4.
    Wade

  17. #17
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    I'm using the same OS as you. NT4, SP6 with VB6.0,SP4.

    I did a straight copy and paste into my form code, but still no success.

    I might experiment with it some early next week.

    Thanks.

  18. #18
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    736
    If I create a totally new project and copy your code in, the results are perfect.

    Must be something about the existing project I am trying to add the code to, even though the startup form just contains logon code.

    So, your code works great. I'll just need to keep trying.

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