Results 1 to 21 of 21

Thread: How can I tell if the program is already running?

Hybrid View

  1. #1

  2. #2
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Re: How can I tell if the program is already running?

    I find this to be more versatile than App.PrevInstance.
    IE: not only will it check for previous running instances but will bring the app to the foreground if it had been minimized.

    You can easily modify it to do other stuff besides just that.
    Please compile before running

    Cheers!
    VB Code:
    1. 'Form code
    2. Option Explicit
    3.  
    4. Private Sub Form_Load()
    5.     Call CheckAppPrevInstance
    6. End Sub
    7.  
    8. 'In a module
    9. Option Explicit
    10.  
    11. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    12.          (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    13. Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
    14.          ByVal wCmd As Long) As Long
    15. Private Declare Function OpenIcon Lib "user32" (ByVal hWnd As Long) As Long
    16. Public Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
    17. Private Const GW_HWNDPREV = 3
    18.  
    19. Public Sub CheckAppPrevInstance()
    20.     Dim OldTitle As String
    21.     Dim ll_WindowHandle As Long
    22.      
    23.     'saving the current title in OldTitle variable
    24.     'and changing the application title so it will not look for itself
    25.     OldTitle = App.Title
    26.    
    27.     App.Title = "Dup instance"
    28.    
    29.     'finding the previous instance.
    30.     ll_WindowHandle = FindWindow("ThunderRT6Main", OldTitle)
    31.     'if there is no old instances of your application - exit.
    32.        
    33.     If ll_WindowHandle = 0 Then App.Title = OldTitle: Exit Sub
    34.    
    35.     'Find the window we need to restore
    36.     ll_WindowHandle = GetWindow(ll_WindowHandle, GW_HWNDPREV)
    37.      
    38.     'Now restore it
    39.     Call OpenIcon(ll_WindowHandle)
    40.     'And Bring it to the foreground
    41.     Call SetForegroundWindow(ll_WindowHandle)
    42.              
    43.     End
    44.          
    45. End Sub
    Give your music collection a whole new life with PartyTime Jukebox

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: How can I tell if the program is already running?

    For another user, or on another machine?

  4. #4
    Fanatic Member daydee's Avatar
    Join Date
    Jun 2001
    Location
    Canada
    Posts
    560

    Re: How can I tell if the program is already running?

    Quote Originally Posted by dglienna
    For another user, or on another machine?
    Sorry can't answer that one right now but will try it out first chance I get.
    Give your music collection a whole new life with PartyTime Jukebox

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: How can I tell if the program is already running?

    Quote Originally Posted by daydee
    I find this to be more versatile than App.PrevInstance.
    IE: not only will it check for previous running instances but will bring the app to the foreground if it had been minimized.
    VB Code:
    1. .
    2. .
    3. .
    4.     'finding the previous instance.
    5.     ll_WindowHandle = FindWindow("ThunderRT6Main", OldTitle)
    6.     'if there is no old instances of your application - exit.
    7. .
    8. .
    9. .
    Exactly what I was looking for - I was just wanting to change my App.PrevInstance code to actually find and bring the window to the front...

    What is the ThunderRT6Main part mean? The OldTitle variable already contains the name of the app - so I'm unclear on Thunder part...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

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