Results 1 to 14 of 14

Thread: Preventing more than one instance of app running

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    South Africa
    Posts
    105

    Preventing more than one instance of app running

    Hi there

    Is there any way to prevent more than one instance of one's application to run at the same time? Is there any way to check when the application loads if an instance of the app is already running and if it is then don't load the application?

    Microsoft seems to do this with Office applications.

    Many thanks

    langals

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

    Re: Preventing more than one instance of app running

    In your SubMain or Form_Load....

    VB Code:
    1. If App.PrevInstance = True Then
    2.     MsgBox "Application is already running..."
    3.     End
    4. End If
    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

  3. #3
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Preventing more than one instance of app running

    Quote Originally Posted by langals
    Hi there

    Is there any way to prevent more than one instance of one's application to run at the same time? Is there any way to check when the application loads if an instance of the app is already running and if it is then don't load the application?

    Microsoft seems to do this with Office applications.

    Many thanks

    langals
    Try something like this:

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim frmMyForm As Form
    3.     If App.PrevInstance = True Then
    4.         For Each frmMyForm In Forms
    5.             Unload frmMyForm
    6.             Set frmMyForm = Nothing
    7.         Next frmMyForm
    8.         Set frmMyForm = Nothing
    9.         End
    10.     End If
    11. End Sub

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

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

    Re: Preventing more than one instance of app running

    Quote Originally Posted by sciguyryan
    Private Sub Form_Load()
    Dim frmMyForm As Form
    If App.PrevInstance = True Then
    For Each frmMyForm In Forms
    Unload frmMyForm
    Set frmMyForm = Nothing
    Next frmMyForm
    Set frmMyForm = Nothing

    End
    End If
    End Sub
    I think the bold ones are unnecessary since the app is still loading, thus I assume no form has been loaded yet......
    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

  5. #5
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Preventing more than one instance of app running

    Quote Originally Posted by dee-u
    I think the bold ones are unnecessary since the app is still loading, thus I assume no form has been loaded yet......

    Probably but I always leave them there just to be shure. Unless you are looking for a very snappy shutdown (Which that code will not affect by much in any case) it should work all the same

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

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

    Re: Preventing more than one instance of app running

    Try also this link.
    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

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Preventing more than one instance of app running

    Quote Originally Posted by dee-u
    Try also this link.
    Nice use of a mutex there. I'd say that's probably the best way to do it, out of the ways I've seen so far (registry flag etc.).

    If your app is one that loads data (like a document reader), just quitting if there is an existing instance probably isn't enough. Every time you open a Word document from Explorer, you are opening an instance of Word. If there is already an instance, it sends the document path back to the original instance before it quits, otherwise the document would never get opened. There are a variety of ways you can do this. One way (probably the simplest) is shown in the "Sending strings between VB apps" link in my sig.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    South Africa
    Posts
    105

    Re: Preventing more than one instance of app running

    Thanks for the help guys - that did the trick.

    langals

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

    Re: Preventing more than one instance of app running

    This alternative might be noteworthy too....

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. Public Sub Main()
    3.     Dim m_hWnd As Long
    4.  
    5.     'Change Form1 to your form Caption.
    6.     m_hWnd = FindWindow(vbNullString, "Form1")
    7.     If m_hWnd > 0 Then
    8.         MsgBox "Program " & App.Title & " is already running..."
    9.         Unload Form1
    10.         Exit Sub
    11.     End If
    12.  
    13.     'Change Form1 to your form name.
    14.     Form1.Show
    15. End Sub
    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

  10. #10
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Preventing more than one instance of app running

    Quote Originally Posted by dee-u
    This alternative might be noteworthy too....

    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. Public Sub Main()
    3.     Dim m_hWnd As Long
    4.  
    5.     'Change Form1 to your form Caption.
    6.     m_hWnd = FindWindow(vbNullString, "Form1")
    7.     If m_hWnd > 0 Then
    8.         MsgBox "Program " & App.Title & " is already running..."
    9.         Unload Form1
    10.         Exit Sub
    11.     End If
    12.  
    13.     'Change Form1 to your form name.
    14.     Form1.Show
    15. End Sub
    I am trying to use your code with a login form, the prompt comes up to say its already loaded but it also logs you in without credentials... ugh.
    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    2. (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    3.  
    4. Public Sub Main()
    5.     Dim m_hWnd As Long
    6.  
    7.     'Change Form1 to your form Caption.
    8.     m_hWnd = FindWindow(vbNullString, "Login to Client Manager Pro")
    9.     If m_hWnd > 0 Then
    10.         frmAppAlreadyRunning.Show vbModal
    11.         Unload frmLoginForm
    12.         Exit Sub
    13.     Else
    14.         frmLoginForm.Show
    15.     End If
    16. End Sub

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

    Re: Preventing more than one instance of app running

    Put it in sub Main before the frmLogin ever gets called.

  12. #12
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Preventing more than one instance of app running

    Quote Originally Posted by dglienna
    Put it in sub Main before the frmLogin ever gets called.
    tried that too and when you click on the desktop icon. nothing loads, nothing checks, nothing happens.

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

    Re: Preventing more than one instance of app running

    This only runs the code if it's registered, and doesn't allow it to be executed twice. It is a case that you have to use END.

    Post the code that you have. Sounds like the app is still running in the background.

    VB Code:
    1. Public Sub Main()
    2.     If App.PrevInstance Then
    3.      Beep
    4.      End
    5.     Else
    6.       GetAppEnvironment
    7.       If Not gRegClass.Registered Then
    8.         frmRegister.Show vbModal
    9.         End
    10.       Else
    11.         frmBackground.Show
    12.       End If
    13.     End If
    14. End Sub

  14. #14
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Preventing more than one instance of app running

    Quote Originally Posted by dglienna
    This only runs the code if it's registered, and doesn't allow it to be executed twice. It is a case that you have to use END.

    Post the code that you have. Sounds like the app is still running in the background.

    VB Code:
    1. Public Sub Main()
    2.     If App.PrevInstance Then
    3.      Beep
    4.      End
    5.     Else
    6.       GetAppEnvironment
    7.       If Not gRegClass.Registered Then
    8.         frmRegister.Show vbModal
    9.         End
    10.       Else
    11.         frmBackground.Show
    12.       End If
    13.     End If
    14. End Sub
    ill try it and get back to you

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