Check to see if application is open ??

Discussion in 'AutoCAD' started by Dave Wilkinson, Nov 18, 2004.

  1. Can someone help me out with what I am sure is a simple task ???

    I need to fire up an application from within VB (its a .exe in a known
    location) . I have some code that will do this and seems to work, but if the
    application is already running then it just fires up another session....
    How can I check to see if the application already running, and if so just
    switch to that application ???

    Many Thanks
    Davey
     
    Dave Wilkinson, Nov 18, 2004
    #1
  2. Dave Wilkinson

    Mikko Guest

    ' 1. Add a new module OnlyOne.vb to your WinForm application and Cut and paste this into the module.
    ' 2. Change the 'YourAppName' as appropriate
    ' 3. Change the name of the form to start the application with [Application.Run(New MainForm)]
    ' 4. Change the StartupObject in the project properties to Main()

    Imports System.Diagnostics

    Module OnlyOne

    Public Sub Main()

    If CheckForDuplicateProcess("YourAppName") Then
    Dim dupProcess As String
    dupProcess = "There is another instance running on this machine." & vbCrLf & "This new instance must close."
    MsgBox(dupProcess, MsgBoxStyle.Critical, "Duplicate Process Detected")
    Application.Exit()
    Else
    ' Change The FormName Below
    ' Change the name of the form to load, to your application
    Application.Run(New MainForm)
    End If
    End Sub

    Private Function CheckForDuplicateProcess(ByVal processName As String) As Boolean
    Dim Procs() As Process
    Dim proc As Process
    Procs = Process.GetProcesses()
    Dim count As Integer = 0
    For Each proc In Procs
    If proc.ProcessName.ToString.Equals(processName) Then
    count += 1
    End If
    Next proc
    If count > 1 Then
    Return True
    Else
    Return False
    End If

    End Function

    End Module
     
    Mikko, Nov 18, 2004
    #2
  3. Hi Mikko,

    I have tried the code but have problems with the variables defined as
    "Process" ! My installation of either AutoCAD, Inventor or VB6 does not
    recognise this type of variable ???
    Is there a refference that needs to be added in ???

    Thanks
    Davey


    [Application.Run(New MainForm)]
    & "This new instance must close."
     
    Dave Wilkinson, Nov 19, 2004
    #3
  4. Dave Wilkinson

    Mikko Guest

    This is VB.NET code, I don't believe that it will work with VB6. Here is a link to get a free beta VB.NET 2005 from Microsoft if you feel like stepping up. I'm using 2003 but you have to buy that now. http://lab.msdn.microsoft.com/express/vbasic/
     
    Mikko, Nov 19, 2004
    #4
  5. Dave Wilkinson

    pkirill Guest

    Don't know if it will help with your app, but here's the VB6 snippet I use
    to test if Acad is open. (Much thanks to those in this NG that helped me get
    here)


    Public blnAcadExist As Boolean
    Public dwg As AcadDocument
    Public Acad As AcadApplication

    Sub DefineAcadSession()

    AcadExist = False
    App.OleRequestPendingTimeout = 999999
    On Error Resume Next
    Set Acad = GetObject(, "AUTOCAD.APPLICATION")
    If Err Then
    Err.Clear
    Set Acad = New AcadApplication
    Else
    AcadExist = True
    End If

    End Sub
     
    pkirill, Nov 19, 2004
    #5
  6. Dave Wilkinson

    Mikko Guest

    Mikko, Nov 19, 2004
    #6
  7. Hi Mikko / Pkrill

    I have included the code that I am using below that opens my application.. I
    have also tried to addapt the code samples you have given to suit this but
    have had no joy ! Both pieces of code make use of the "App" class which is
    not available to me in either Autocad 2002 or Inventor 8..
    I must admit that I thought this would have been a fairly easy case of just
    recursing through the process's running looking for "PDM.exe" and if true
    then make that windowcurrent....
    As with so many things in VB / VBA I find that the straight forward things
    (or what I thought would have been) seem to be anything but, although I
    suspect this is down to my lack of knowledge with VB..

    Sub openPDM()


    Set objShell = CreateObject("wscript.shell")
    objShell.Run ("""\\<server>\pdm\pdm.exe""")

    End Sub


    If you could offer any further advice on this subject it would be very
    helpfull.
    Many Thanks
    Dave
     
    Dave Wilkinson, Nov 22, 2004
    #7
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.