VB.Net – Excel COM对象没有得到释放

即使在调用ReleaseComObject和GC.Collect方法之后,我仍然面临Excel进程仍处于活动状态的问题。

我的Excel进程终止,但只有在closures用户窗体后

下面是示例代码,它显示了我所做的所有事情摆脱Excel进程:

Public Class frmTEST Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim objExcel As xl.Application Dim wbReport As xl.Workbook = Nothing objExcel = CreateObject("Excel.Application") Try wbReport = objExcel.Workbooks.Open("D:\EL\Nicolas\VS Online\Classe A v2\Launcher-v2.2\Resources\Modules\Zoom.xlsm") Catch ex As Exception Common.WriteDebugLog("Exception line 44") End Try If wbReport Is Nothing Then MsgBox("Erreur d'ouverture du reporting - Code 745.", vbExclamation) Exit Sub End If With objExcel .Visible = False .ScreenUpdating = False .Calculation = xl.XlCalculation.xlCalculationManual .DisplayAlerts = False End With '' Here I do all my processing which I have removed to make the question more simplified With objExcel .Calculation = xl.XlCalculation.xlCalculationAutomatic .ScreenUpdating = True .DisplayAlerts = True End With ''~~> Close & Clean Up wbReport.Close(SaveChanges:=False) objExcel.Quit() Me.ReleaseObject(wbReport) Me.ReleaseObject(objExcel) MsgBox("Done") End Sub Private Sub ReleaseObject(ByVal obj As Object) Try Dim intRel As Integer = 0 Do intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) Loop While intRel > 0 MsgBox("Final Released obj # " & intRel) Catch ex As Exception MsgBox("Error releasing object" & ex.ToString) obj = Nothing Finally GC.Collect() End Try End Sub End Class 

更新 :根据我收到的意见,我在其他线程后 ,我的代码进行了更改,但它仍然没有帮助。 我的Excel进程终止,但只有在closures用户窗体后

如果您使用的是.Net V4或更高版本,请尝试一下。 将所有Button1_Click代码移动到子例程中,并从Button1_Click调用它。 这将允许该子例程本地的对象超出范围,从而有资格进行垃圾收集。

然后调用一个使用Marshal.AreComObjectsAvailableForCleanup函数的清理方法来确定释放COM对象需要多less个垃圾收集周期。

备注

如果托pipe代码和本地代码之间的深度依赖关系图有很多引用,则清理所有对象可能需要很长时间。 GC每运行一次,它将释放一些RCW,这又将释放底层的COM对象。 这些COM对象然后将释放它们的托pipe引用,并在GC运行时使更多的对象可用于清除,这将再次启动该过程。

AreComObjectsAvailableForCleanup方法为应用程序提供了一种方法来确定GC.Collect和GC.WaitForPendingFinalizers需要发生多less次循环才能清理所有内容。

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ExcelWork() Cleanup() End Sub Private Sub ExcelWork() Dim objExcel As xl.Application Dim wbReport As xl.Workbook = Nothing objExcel = CreateObject("Excel.Application") Try wbReport = objExcel.Workbooks.Open("D:\EL\Nicolas\VS Online\Classe A v2\Launcher-v2.2\Resources\Modules\Zoom.xlsm") Catch ex As Exception Common.WriteDebugLog("Exception line 44") End Try If wbReport Is Nothing Then MsgBox("Erreur d'ouverture du reporting - Code 745.", vbExclamation) Exit Sub End If With objExcel .Visible = False .ScreenUpdating = False .Calculation = xl.XlCalculation.xlCalculationManual .DisplayAlerts = False End With '' Here I do all my processing which I have removed to make the question more simplified With objExcel .Calculation = xl.XlCalculation.xlCalculationAutomatic .ScreenUpdating = True .DisplayAlerts = True End With ''~~> Close & Clean Up wbReport.Close(SaveChanges:=False) objExcel.Quit() MsgBox("Done") End Sub Private Sub Cleanup() Do GC.Collect() GC.WaitForPendingFinalizers() Loop While Marshal.AreComObjectsAvailableForCleanup End Sub