Results 1 to 5 of 5

Thread: application already running?!?!

  1. #1

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    application already running?!?!

    hi all!!

    how can i check if my application is already running or not?
    if it is then how can i display that "the application is already running."??

    thnx

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: application already running?!?!

    App.PrevInstance, will detect if it is already running

    you can then display a message box, or exit the program

    pete

  3. #3

    Thread Starter
    Smitten by reality Harsh Gupta's Avatar
    Join Date
    Feb 2005
    Posts
    2,938

    Re: application already running?!?!

    thnx westconn1 !!!

  4. #4
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796

    Re: application already running?!?!

    A neater option if a previous instance of the application is running is just to switch to it:

    VB Code:
    1. Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
    2. Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    3. (ByVal lpClassName As String, ByVal lpWindowName As String) _
    4. As Long
    5. Declare Function GetWindow Lib "user32" _
    6. (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    7. Declare Function SetForegroundWindow Lib "user32" _
    8. (ByVal hwnd As Long) As Long
    9.  
    10. Public Sub Main()
    11.  
    12.   If App.PrevInstance Then
    13.     ActivatePrevInstance
    14.   End If
    15.  
    16.   'Etc......
    17.  
    18. End sub
    19.  
    20. Public Sub ActivatePrevInstance()
    21.  
    22.   Dim OldTitle As String, PrevHndl As Long
    23.   Dim Result As Long
    24.  
    25.   'Save the title of the application.
    26.   OldTitle = App.Title
    27.  
    28.   'Rename the title of this application so FindWindow
    29.   'will not find this application instance.
    30.   App.Title = "Duplicate instance"
    31.  
    32.   'Attempt to get window handle using VB4 class name.
    33.   PrevHndl = FindWindow("ThunderRTMain", OldTitle)
    34.  
    35.   'Check for no success.
    36.   If PrevHndl = 0 Then
    37.  
    38.     'Attempt to get window handle using VB5 class name.
    39.     PrevHndl = FindWindow("ThunderRT5Main", OldTitle)
    40.  
    41.   End If
    42.  
    43.   'Check if found
    44.   If PrevHndl = 0 Then
    45.  
    46.     'Attempt to get window handle using VB6 class name
    47.     PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
    48.  
    49.   End If
    50.  
    51.   'Check if found
    52.   If PrevHndl = 0 Then
    53.  
    54.     'No previous instance found.
    55.     Exit Sub
    56.  
    57.   End If
    58.  
    59.   'Get handle to previous window.
    60.   PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)
    61.  
    62.   'Restore the program.
    63.   Result = OpenIcon(PrevHndl)
    64.  
    65.   'Activate the application.
    66.   Result = SetForegroundWindow(PrevHndl)
    67.  
    68.   'End the application.
    69.   End
    70.  
    71. End Sub
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: application already running?!?!

    Take a look at this too.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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