Single Application Run

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Rogers

    Single Application Run

    How do I make it so that once my application (.exe) is running, if the user
    tries to run a second instance of the program, it is not allowed.

    I want my program to be able to have only one instance of it running,
    period.

    Thx,

    -Tom



  • Karl E. Peterson

    #2
    Re: Single Application Run

    Tom Rogers wrote:
    How do I make it so that once my application (.exe) is running, if
    the user tries to run a second instance of the program, it is not
    allowed.
    >
    I want my program to be able to have only one instance of it running,
    period.
    There are quite a number of ways. The simplest is something like this:

    Sub Main
    If App.PrevInstanc e Then Exit Sub
    ...

    If you know the caption of your app, you can enhance this like:

    Sub Main
    If App.PrevInstanc e Then
    AppActivate MyCaption
    Exit Sub
    End If
    ...

    For more ideas, see http://vb.mvps.org/samples/PrevInst
    --


    Comment

    • Henning

      #3
      Re: Single Application Run

      Google is a good friend :)
      From VBForums-ClassicVB by dee-u.

      Method 1: You can read the PrevInstance property of the App object, if the
      value of this is True then another instance of the application is already
      running.

      If your program starts with Sub Main, you can use this code to exit the
      program if another copy is already running:
      visual basic
      code:-----------------------------------------------------------------------
      -------'in sub main...
      If App.PrevInstanc e = True Then
      MsgBox "Already running...."
      Exit Sub
      End If
      ----------------------------------------------------------------------------
      --For forms you need an extra piece of code to also close the form, the
      following code should be placed in Form_load:
      visual basic
      code:-----------------------------------------------------------------------
      -------'in form_load...
      If App.PrevInstanc e = True Then
      MsgBox "Already running...."
      Unload Me
      Exit Sub
      End If
      ----------------------------------------------------------------------------
      --

      Method 2: You can use the FindWindow API to search for open windows with
      your form caption.

      Note that for this to work you must know the exact title to find, so if you
      change your window title within your program

      (such as adding a file name, like Notepad does) then this will fail. Also,
      if other running programs have the same caption,

      your program may not run at all.

      visual basic
      code:-----------------------------------------------------------------------
      -------
      Option Explicit

      Private Declare Function FindWindow Lib "user32" Alias "FindWindow A" (ByVal
      lpClassName As String, ByVal lpWindowName As String) As Long

      Public Sub Main()
      Dim m_hWnd As Long

      'Change Form1 to your form Caption.
      m_hWnd = FindWindow(vbNu llString, "Form1")
      If m_hWnd 0 Then
      MsgBox "Program " & App.Title & " is already running..."
      Exit Sub
      End If

      'Change Form1 to your form name.
      Form1.Show
      End Sub
      /Henning"Tom Rogers" <jeditom@NO_SPA Mtwcny.rr.comsk rev i meddelandet
      news:6Nxtg.8536 4$3B.44128@twis ter.nyroc.rr.co m...
      How do I make it so that once my application (.exe) is running, if the
      user
      tries to run a second instance of the program, it is not allowed.
      >
      I want my program to be able to have only one instance of it running,
      period.
      >
      Thx,
      >
      -Tom
      >
      >
      >

      Comment

      • Henning

        #4
        Re: Single Application Run

        Untested


        /Henning

        "Tom Rogers" <jeditom@NO_SPA Mtwcny.rr.comsk rev i meddelandet
        news:6Nxtg.8536 4$3B.44128@twis ter.nyroc.rr.co m...
        How do I make it so that once my application (.exe) is running, if the
        user
        tries to run a second instance of the program, it is not allowed.
        >
        I want my program to be able to have only one instance of it running,
        period.
        >
        Thx,
        >
        -Tom
        >
        >
        >

        Comment

        Working...