0

I am new to VB.net and I am trying to write a forms app that works with Autodesk Inventor.

Unfortunately, every time I close Inventor, the "Inventor.exe" process still stays. I didn't realize this while debugging and I only realized this when I checked the task manager after the whole system started to lag. Killing every process with the same name is fairly simple, but the issue is that the end user might have separate documents open in another Inventor window. So I need to write a function that only kills the Inventor processes that don't have a window open.

Public Class Form1
    Dim _invApp As Inventor.Application
    Dim _started As Boolean = False
    Public Sub New()

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.
    Try
        _invApp = Marshal.GetActiveObject("Inventor.Application")

    Catch ex As Exception
        Try
            Dim invAppType As Type = _
              GetTypeFromProgID("Inventor.Application")

            _invApp = CreateInstance(invAppType)
            _invApp.Visible = True

            'Note: if you shut down the Inventor session that was started
            'this(way) there is still an Inventor.exe running. We will use
            'this Boolean to test whether or not the Inventor App  will
            'need to be shut down.
            _started = True

        Catch ex2 As Exception
            MsgBox(ex2.ToString())
            MsgBox("Unable to get or start Inventor")
        End Try
    End Try

End Sub

Another section where I start a process, which is a specific 3D model file.

Public Sub SaveAs(oTemplate As String)
    'define the active document
    Dim oPartDoc As PartDocument = _invApp.ActiveDocument
    'create a file dialog box
    Dim oFileDlg As Inventor.FileDialog = Nothing
    Dim oInitialPath As String = System.IO.Path.GetFullPath("TemplatesResources\" & oTemplate)
    _invApp.CreateFileDialog(oFileDlg)

    'check file type and set dialog filter
    If oPartDoc.DocumentType = kPartDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Part Files (*.ipt)|*.ipt"
    ElseIf oPartDoc.DocumentType = kAssemblyDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
    ElseIf oPartDoc.DocumentType = kDrawingDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Drawing Files (*.idw)|*.idw"
    End If

    If oPartDoc.DocumentType = kAssemblyDocumentObject Then
        oFileDlg.Filter = "Autodesk Inventor Assembly Files (*.iam)|*.iam"
    End If

    'set the directory to open the dialog at
    oFileDlg.InitialDirectory = "C:\Vault WorkSpace\Draft"
    'set the file name string to use in the input box
    oFileDlg.FileName = "######-AAAA-AAA-@@"

    'work with an error created by the user backing out of the save
    oFileDlg.CancelError = True
    On Error Resume Next
    'specify the file dialog as a save dialog (rather than a open dialog)
    oFileDlg.ShowSave()

    'catch an empty string in the imput
    If Err.Number <> 0 Then
        MessageBox.Show("Any changes made from here will affect the original template file!", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
    ElseIf oFileDlg.FileName <> "" Then
        Dim MyFile As String = oFileDlg.FileName
        'save the file
        oPartDoc.SaveAs(MyFile, False)
        'open the drawing document
        System.Diagnostics.Process.Start(oInitialPath & ".idw")
        Dim oFinalPath As String = oPartDoc.FullFileName
        MessageBox.Show(oFinalPath, "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
        MessageBox.Show("Loaded", "", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification)
        Dim oDrawingDoc As DrawingDocument = _invApp.Documents.ItemByName(oInitialPath & ".idw")
        'oDrawingDoc.SaveAs()

    End If
End Sub

Any help is appreciated. Thanks

Maxence
  • 12,868
  • 5
  • 57
  • 69
RBuntu
  • 907
  • 10
  • 22
  • 1
    Better trying to understand why your process doesn't terminate when your user closes it, – Steve Jul 23 '15 at 21:45
  • When my form opens up, it opens inventor as well. I copied the code from somewhere else. I posted it above, as the Public Sub New(). I will also post a part of my code where I start another process that opens up a specific file into inventor. – RBuntu Jul 23 '15 at 21:50
  • 1
    I don't see where you are disposing the `Inventor.Application` object. Could that be the problem? – OneFineDay Jul 23 '15 at 22:07
  • Im not sure how to do that. Like I said, I am completely new to VB. – RBuntu Jul 24 '15 at 13:13

1 Answers1

1

At the end on your form, probably FormClosed, add the following:

  Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
    If (_invApp Is Nothing) Then Return ' if empty, then no action needed
    _invApp.Quit()
    Marshal.ReleaseComObject(_invApp)
    _invApp = Nothing
    System.GC.WaitForPendingFinalizers()
    System.GC.Collect()
  End Sub

This should make .NET release and properly dispose the COM Object for Inventor.

And consider declare the Inventor variable and assign Nothing/null, it's safer (avoid mistakes)

  Dim _invApp As Inventor.Application = Nothing
Augusto Goncalves
  • 8,493
  • 2
  • 17
  • 44
  • This doesnt seem to work for me. Heres what I am using. http://pastebin.com/QK9Jb6ND – RBuntu Jul 24 '15 at 14:02
  • Sorry I forgot the Close() before ReleasseComObject. Basically you need to close the app then tell it that you don't need it anymore (release) – Augusto Goncalves Jul 24 '15 at 14:48
  • But wouldnt that tell the form to close on its own? I only want the program to kill any "inventor.exe" processes that dont have a window. And I only want the program to execute that when it is manually closed by the user. – RBuntu Jul 24 '15 at 21:26
  • Sorry, I meant call Close on the _invApp, the ReleaseCom, then =null. – Augusto Goncalves Jul 24 '15 at 23:00
  • I'm sorry, I'm very new to VB. Do you think you could point me to an example of what you mean? – RBuntu Jul 27 '15 at 14:10
  • ok I made some changes on the reply and tested on my end, working as expected. – Augusto Goncalves Jul 28 '15 at 18:09
  • Im sorry if I wasnt clear with my needs but this isnt doing what I need. Your code is causing the application to exit when the form is closed. I need it to only kill the processes that dont have windows.My user should be able to close the form without causing the application to exit, because the form is only used in the beginning. Heres what I ended up using. http://pastebin.com/1u9dPtGt – RBuntu Jul 28 '15 at 18:30