When If App.PrevInstance doesn't detect a previous instance
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: When If App.PrevInstance doesn't detect a previous instance

Hybrid View

  1. #1
    Join Date
    Apr 2003
    Posts
    322

    When If App.PrevInstance doesn't detect a previous instance

    I've found this code in this forum

    Private sub Form_Load()
    'see if there is an istance already
    If App.PrevInstance Then
    msgbox "Application is already running"
    AppActivate App.Title
    'send the key (here SHIFT key) to set the form from the Prev Instance to the top
    SendKeys "+", True

    'Terminate the new instance
    Unload Me
    End if
    End sub

    However, It doesn't appear to work for me.
    (I don't care about the SendKeys() and AppActivate() calls, so I'm down to this)

    Private sub Form_Load()
    'see if there is an istance already
    If App.PrevInstance Then
    'Terminate the new instance
    Unload Me
    End if
    End sub



    I've tried running this


    If App.PrevInstance Then
    'Terminate the new instance
    Exit Sub
    End if


    as the first line of code in my main() routine as well, and it just does not detect another instance of the program running.

    What would cause this ?

  2. #2
    Join Date
    Apr 2004
    Posts
    14
    did you put the code in sub main ? have you set the startup object to sub main ? there is no problem with app.previnstance.

  3. #3
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    If you have the app running inside the IDE it won't pick up the prevInstance.
    Be nice to Harley riders...

  4. #4
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    It wont also....

    If you make a copy of the exe in a different folder, and launch the two copy of same exe, they will not know of each other)
    Or even if you make a copy of exe in same foder (with a different filename name) and run, they will not know they are the same...
    Last edited by Cimperiali; May 5th, 2004 at 03:55 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  5. #5
    Join Date
    Jun 2003
    Location
    Planet Earth
    Posts
    186
    Also keep in mind that Windows NT can support multiple desktops and if you use an exe designed to work with distributed COM, even more funky stuff can occur.

  6. #6
    Join Date
    Apr 2003
    Posts
    322

    Re: It wont also....

    Originally posted by Cimperiali
    If you make a copy of the exe in a different folder, and launch the two copy of same exe, they will not know of each other)
    Or even if you make a copy of exe in same foder (with a different filename name) and run, they will not know they are the same...


    So what needs to be done to make this mechanism work ?

  7. #7
    Join Date
    Apr 2003
    Posts
    322
    Originally posted by hendramin
    did you put the code in sub main ? have you set the startup object to sub main ? there is no problem with app.previnstance.
    As posted in my original message, I have tried this as the first few lines in main(), as well as in form_load() of the first form which is loaded.

    No matter what I've tried, it doens't seem to work- so obviously, I'm missing something crucial :-)

  8. #8
    Join Date
    Apr 2003
    Posts
    322
    Originally posted by Twodogs
    If you have the app running inside the IDE it won't pick up the prevInstance.
    I had originally tried it inside of the IDE, but then I compiled 2 identical EXE's, with different filenames.

    When I run those 2 exe's, they still run as if the App.Previnstance code is not even there, so obviously I am doing somehting wrong

  9. #9
    Join Date
    Apr 2003
    Posts
    322
    Originally posted by hendramin
    did you put the code in sub main ? have you set the startup object to sub main ? there is no problem with app.previnstance.
    Hmm. I'm not sure how to explicitly set the "startup object to sub main", but main() is called before any of the formas (as verified with a breakpoint). Maybe this is the default??

    How does one explicitly set main() to be called as the startup objec t?

  10. #10
    Join Date
    Jun 2003
    Location
    Planet Earth
    Posts
    186
    How does one explicitly set main() to be called as the startup objec t?
    Go to Project>>Project Properties>>General>>Startup Object and set to Sub Main

    Have you stepped though the code to see if in fact the .previnstance is false?

  11. #11
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,209

    Re: It wont also....

    Originally posted by Cimperiali
    If you make a copy of the exe in a different folder, and launch the two copy of same exe, they will not know of each other)
    Or even if you make a copy of exe in same foder (with a different filename name) and run, they will not know they are the same...
    I did not realize this before !!
    seems that PrevInstance checks if the EXE module is loaded or not..
    So this does not work if you make copies of the EXE

    anyway this is my solution for this prolem :

    clsMutex Code :
    Code:
    Option Explicit
    
    Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    Private Type SECURITY_ATTRIBUTES
        nLength As Long
        lpSecurityDescriptor As Long
        bInheritHandle As Long
    End Type
    
    Private Const ERROR_ALREADY_EXISTS = 183&
    
    Private m_hMutex As Long
    
    Public Function CheckMutex(MutexName As String) As Boolean
    
    Dim s As SECURITY_ATTRIBUTES
    m_hMutex = CreateMutex(s, 0, MutexName)
    
    If Err.LastDllError = ERROR_ALREADY_EXISTS Then
        CheckMutex = False
    Else
        CheckMutex = True
    End If
    
    End Function
    
    
    Private Sub Class_Terminate()
    CloseHandle m_hMutex
    End Sub
    Form code:
    Code:
    Option Explicit
    Dim mut As clsMutex
    
    Private Sub Form_Load()
    Set mut = New clsMutex
    If Not mut.CheckMutex("CodeGuru") Then 'Case sensitive string
        MsgBox "Application is running"
        Unload Me
    End If
    
    End Sub
    How does it work ?
    it creates a mutex (shared object that hace a uniqe name)..
    if you try to recreate it you get the error :ERROR_ALREADY_EXISTS
    this is the whole idea..But remember that mut (the clsMutex instance) must be alive as long as you application is still running because the mutex is close in the terminate event.
    try it
    Attached Files Attached Files

  12. #12
    Join Date
    Apr 2002
    Location
    Melbourne, Victoria, Australia
    Posts
    1,792
    Originally posted by cappy2112
    but then I compiled 2 identical EXE's, with different filenames.
    App.PrevInstance will look to see if the actual application is already running - not whether you have another exe running at the same time. ie - 2 copies of the same program running.
    Be nice to Harley riders...

  13. #13
    Join Date
    Apr 2003
    Posts
    322
    Originally posted by rocket
    Go to Project>>Project Properties>>General>>Startup Object and set to Sub Main

    Have you stepped though the code to see if in fact the .previnstance is false?
    yes- but it was because I was trying to run 2 copies (2 exe's) of the same program.

    Now I'll have to try this again- the right way :-)

  14. #14
    Join Date
    Apr 2003
    Posts
    322
    Originally posted by rocket
    Go to Project>>Project Properties>>General>>Startup Object and set to Sub Main

    Have you stepped though the code to see if in fact the .previnstance is false?
    Very useful. I didn't use this before.

  15. #15
    Join Date
    Apr 2003
    Posts
    322
    Originally posted by Twodogs
    App.PrevInstance will look to see if the actual application is already running - not whether you have another exe running at the same time. ie - 2 copies of the same program running.
    Ok- App.Previnstance works for me now !!

    thanks everyone!!

Page 1 of 2 12 LastLast

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center