.net - VB.Net - Excel COM 对象未发布

标签 .net vb.net excel com interop

这个问题在这里已经有了答案:





Application not quitting after calling quit

(11 个回答)


6年前关闭。




我面临一个问题,即使在调用 ReleaseComObject 和 GC.Collect 方法后,Excel 进程仍然处于事件状态。

我的 Excel 进程终止,但仅在我关闭用户表单 后才终止

下面是示例代码,它显示了我为摆脱 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

UPDATE: Based on comments I received, I made changes in my code following the other thread, but it still doesn't help. My Excel Process terminates but ONLY after I close User Form

最佳答案

如果您使用的是 .Net V4 或更高版本,请尝试一下。
移动您所有的Button1_Click将代码写入子程序并从 Button1_Click 调用它.这将允许该子例程的本地对象超出范围,从而有资格进行垃圾收集。
然后调用使用 Marshal.AreComObjectsAvailableForCleanup 的清理方法函数来确定释放 COM 对象需要多少垃圾回收周期。

Remarks

If there are a lot of references between managed and native code with deep dependency graphs it can take a long time for all the objects to clean up. Each time a GC runs it will free up some number of RCWs, which will in turn release the underlying COM objects. Those COM objects will then release their managed references and make more objects available for cleanup the next time a GC runs, which starts the process over again.

The AreComObjectsAvailableForCleanup method provides a way for the application to determine how many cycles of GC.Collect and GC.WaitForPendingFinalizers need to happen in order to clean everything up.

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

关于.net - VB.Net - Excel COM 对象未发布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36550446/

相关文章:

.net - .NET下的原子文件复制

asp.net - 由于映射,EF 继承的对象插入失败

asp.net - 关于Email Verification - 有哪些方式

java - 在 Excel 中查找合并的单元格,拆分单元格并将其写入新电子表格?

vba - 使用 VBA 循环不同的单元格范围以在每个单元格范围中编写公式

c# - 非重放热可观察

c# - .NET 堆栈和堆,当我声明一个字符串时会发生什么?

.net - WCF JSON SSL 配置

vb.net - 以编程方式更改 Datagridviewcombobox 的项目 VB.NET

excel - 基于表中的值的条件格式给出 'name range' 错误