Results 1 to 6 of 6

Thread: Is my program running

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2000
    Posts
    20
    How can I tell if my program is already running?
    not all police eat doughnut's and drink coffee.. some of us like milk instead.

  2. #2
    Guest
    if you want the 2nd program to end if its already running, put this in the form_load



    Code:
    If App.PrevInstance = True Then
    End
    End If

  3. #3
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    Put this statement as the first statement in the form load event of your startup form:
    Code:
        If App.PrevInstance Then
            MsgBox "My Program is already running." , _
                   vbExclamation, "Cannot Execute"
            End
        End If
    "It's cold gin time again ..."

    Check out my website here.

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    or you could activate the first instance

    'from Microsoft Databse
    'check for previous instance of app
    '
    Option Explicit

    Private Sub Form_Load()

    If App.PrevInstance Then
    ActivatePrevInstance
    End If

    End Sub

    '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    '2) Add a Standard Module to the Project.
    '3) Paste the following code into the module:

    Option Explicit

    Public Const GW_HWNDPREV = 3
    Declare Function OpenIcon Lib "user32" (ByVal hwnd As Long) As Long
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long

    Sub ActivatePrevInstance()

    Dim OldTitle As String
    Dim PrevHndl As Long
    Dim result As Long

    'Save the title of the application.
    OldTitle = App.Title

    'Rename the title of this application so FindWindow
    'will not find this application instance.
    App.Title = "unwanted instance"

    'Attempt to get window handle using VB4 class name.
    PrevHndl = FindWindow("ThunderRTMain", OldTitle)

    'Check for no success.
    If PrevHndl = 0 Then

    'Attempt to get window handle using VB5 class name.
    PrevHndl = FindWindow("ThunderRT5Main", OldTitle)
    End If

    'Check if found
    If PrevHndl = 0 Then
    'Attempt to get window handle using VB6 class name
    PrevHndl = FindWindow("ThunderRT6Main", OldTitle)
    End If

    'Check if found
    If PrevHndl = 0 Then
    'No previous instance found.
    Exit Sub
    End If

    'Get handle to previous window.
    PrevHndl = GetWindow(PrevHndl, GW_HWNDPREV)

    'Restore the program.
    result = OpenIcon(PrevHndl)

    'Activate the application.
    result = SetForegroundWindow(PrevHndl)

    'End the application.
    End

    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  5. #5
    New Member
    Join Date
    May 2000
    Posts
    14
    The version I pinched of a web site somewhere..
    In Form_Load() ...
    Code:
    Dim SaveTitle as string
    If App.PrevInstance Then
        SaveTitle = App.title
        App.title = "... duplicate instance."
        Me.Caption = "... duplicate instance."
        AppActivate SaveTitle$
        SendKeys "% R", True
        End
    End If
    Shab.

    Code:
    Print WeekDayName(vbMonday)

  6. #6
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Smile How about this...

    Assume you main program's form is frmMain.frm and just create a Main sub procedure and set it as your program startup object.

    Code:
    Option Explicit
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Const SW_SHOWNORMAL = 1
    
    Public Sub Main()
    Dim xAppName$
    If App.PrevInstance Then
        xAppName = "<Your Application Title>"
        xhwnd = FindWindow(0&, xAppName)
        If xhwnd <> 0 Then
            AppActivate xAppName
            xhwnd = ShowWindow(xhwnd, SW_SHOWNORMAL)
            End
        End If
    End If
    
    Load frmMain
    frmMain.Show
    End Sub

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