visual studio 2013 - How do I kill a process that doesn't have any windows in VB.net? -


i new vb.net , trying write forms app works autodesk inventor.

unfortunately, every time close inventor, "inventor.exe" process still stays. didn't realize while debugging , realized when checked task manager after whole system started lag. killing every process same name simple, issue end user might have separate documents open in inventor window. need write function kills inventor processes don't have window open.

public class form1     dim _invapp inventor.application     dim _started boolean = false     public sub new()      ' call required designer.     initializecomponent()      ' add initialization after initializecomponent() call.     try         _invapp = marshal.getactiveobject("inventor.application")      catch ex exception         try             dim invapptype type = _               gettypefromprogid("inventor.application")              _invapp = createinstance(invapptype)             _invapp.visible = true              'note: if shut down inventor session started             'this(way) there still inventor.exe running. use             'this boolean test whether or not inventor app              'need shut down.             _started = true          catch ex2 exception             msgbox(ex2.tostring())             msgbox("unable or start inventor")         end try     end try  end sub 

another section start process, specific 3d model file.

public sub saveas(otemplate string)     'define active document     dim opartdoc partdocument = _invapp.activedocument     'create file dialog box     dim ofiledlg inventor.filedialog = nothing     dim oinitialpath string = system.io.path.getfullpath("templatesresources\" & otemplate)     _invapp.createfiledialog(ofiledlg)      'check file type , set dialog filter     if opartdoc.documenttype = kpartdocumentobject         ofiledlg.filter = "autodesk inventor part files (*.ipt)|*.ipt"     elseif opartdoc.documenttype = kassemblydocumentobject         ofiledlg.filter = "autodesk inventor assembly files (*.iam)|*.iam"     elseif opartdoc.documenttype = kdrawingdocumentobject         ofiledlg.filter = "autodesk inventor drawing files (*.idw)|*.idw"     end if      if opartdoc.documenttype = kassemblydocumentobject         ofiledlg.filter = "autodesk inventor assembly files (*.iam)|*.iam"     end if      'set directory open dialog @     ofiledlg.initialdirectory = "c:\vault workspace\draft"     'set file name string use in input box     ofiledlg.filename = "######-aaaa-aaa-@@"      'work error created user backing out of save     ofiledlg.cancelerror = true     on error resume next     'specify file dialog save dialog (rather open dialog)     ofiledlg.showsave()      'catch empty string in imput     if err.number <> 0         messagebox.show("any changes made here affect original template file!", "warning", messageboxbuttons.ok, messageboxicon.warning, messageboxdefaultbutton.button1, messageboxoptions.servicenotification)     elseif ofiledlg.filename <> ""         dim myfile string = ofiledlg.filename         'save file         opartdoc.saveas(myfile, false)         'open drawing document         system.diagnostics.process.start(oinitialpath & ".idw")         dim ofinalpath 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 drawingdocument = _invapp.documents.itembyname(oinitialpath & ".idw")         'odrawingdoc.saveas()      end if end sub 

any appreciated. thanks

at end on form, formclosed, add following:

  private sub form1_formclosed(sender object, e formclosedeventargs) handles me.formclosed     if (_invapp nothing) return ' if empty, no action needed     _invapp.quit()     marshal.releasecomobject(_invapp)     _invapp = nothing     system.gc.waitforpendingfinalizers()     system.gc.collect()   end sub 

this should make .net release , dispose com object inventor.

and consider declare inventor variable , assign nothing/null, it's safer (avoid mistakes)

  dim _invapp inventor.application = nothing 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -