Der richtige Weg, um Excel com-Objekt mit VB.NET zu entsorgen?
Ich habe folgenden Code (aus dem Online-Tutorial). Der Code funktioniert, aber ich vermute, dass die Art und Weise, das Excel-COM-Objekt zu entsorgen, etwas istnicht richtig. Müssen wir wirklich GC.Collect anrufen? Oder wie kann dieses Excel-COM-Objekt am besten entsorgt werden?
<code>Public Sub t1() Dim oExcel As New Excel.Application Dim oBook As Excel.Workbook = oExcel.Workbooks.Open(TextBox2.Text) 'select WorkSheet based on name Dim oWS As Excel.Worksheet = CType(oBook.Sheets("Sheet1"), Excel.Worksheet) Try oExcel.Visible = False 'now showing the cell value MessageBox.Show(oWS.Range(TextBox6.Text).Text) oBook.Close() oExcel.Quit() releaseObject(oExcel) releaseObject(oBook) releaseObject(oWS) Catch ex As Exception MsgBox("Error: " & ex.ToString, MsgBoxStyle.Critical, "Error!") End Try End Sub Private Sub releaseObject(ByVal obj As Object) Try System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) obj = Nothing Catch ex As Exception obj = Nothing Finally GC.Collect() End Try End Sub </code>