vb.net - (另一个).net Excel 僵尸问题

标签 vb.net excel excel-interop

我有一个程序在后台留下一个 excel 僵尸进程的问题。我已经遵循了这里的所有建议和示例以及 MSDN 试图避免这种情况,但这让我发疯了。谁能发现导致僵尸进程发生的原因?

Imports System
Imports System.Collections
Imports System.IO
Imports Excel = Microsoft.Office.Interop.Excel

Private Sub LoadExcelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 处理 LoadExcelButton.Click
    Dim xlApp As New Excel.Application
    Dim xlBook As Excel.Workbook = Nothing 'instantiated to nothing to help try and avoid zombies
    Dim xlSheet As Excel.Worksheet = Nothing
    Dim STFRange As Excel.Range = Nothing

    Dim Name As String, Easting As Integer, Northing As Integer, tDSpa As Double, Type As String

    Dim NumberSTF As Integer, NumberProperties As Integer, i As Integer, ExcelPath As String

    ExcelPath = Me.ExcelPathTextBox.Text.ToString

    If File.Exists(ExcelPath) = False Then
        MessageBox.Show("Excel file does not exist, exiting.")
        Exit Sub
    End If

    Try

        xlApp.Visible = False

        xlBook = xlApp.Workbooks.Open(ExcelPath) ', , [ReadOnly]:=True

        xlSheet = xlBook.Sheets("STF")

        NumberSTF = xlSheet.UsedRange.Rows.Count - 1 '-1 to account for header
        NumberProperties = xlSheet.UsedRange.Columns.Count

        'create a new collection
        'http://msdn.microsoft.com/en-us/library/xth2y6ft(v=vs.71).aspx
        Dim mySTFCollection As New STFCollection

        For i = 1 To NumberSTF 'rather than a for each loop which would require more excel ranges

            STFRange = xlSheet.Cells(i + 1, 1) '+1 on row to account for header
            Name = STFRange.Value.ToString

            STFRange = xlSheet.Cells(i + 1, 2)
            Easting = CInt(STFRange.Value)

            STFRange = xlSheet.Cells(i + 1, 3)
            Northing = CInt(STFRange.Value)

            STFRange = xlSheet.Cells(i + 1, 4)
            tDSpa = CDbl(STFRange.Value)

            STFRange = xlSheet.Cells(i + 1, 5)
            Type = STFRange.Value.ToString

            Dim objSTF As New STF(Name, Easting, Northing, tDSpa, Type)

            mySTFCollection.Add(objSTF)

        Next i

        GC.Collect()
        GC.WaitForPendingFinalizers()
        GC.Collect()
        GC.WaitForPendingFinalizers()

        ReleaseObject(STFRange)
        STFRange = Nothing
        ReleaseObject(xlSheet)
        xlSheet = Nothing

        xlBook.Close(True, , )
        ReleaseObject(xlBook)
        xlBook = Nothing

        xlApp.Quit()
        ReleaseObject(xlApp)
        xlApp = Nothing

    Catch ex As Exception

        MessageBox.Show(ex.Message)

    End Try
End Sub

Public Sub ReleaseObject(ByVal obj As Object)

    Try
        Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

最佳答案

而不是调用 Close()在您的 Excel 应用程序上,您是否尝试过 Dispose() ?

您可能想尝试在代码的 finally block 中进行清理。

关于vb.net - (另一个).net Excel 僵尸问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11955781/

相关文章:

asp.net - 为什么我的 RouteTable.Routes 属性是 System.Web.Routing.RouteCollection 的实例而不是 System.Web.Http.HttpRouteCollection 的实例?

vb.net - Visual Studio 2012 无法编译 exe,没有代码错误

vb.net - 使用 Entity Framework 返回列表, 'System.Collections.Generic.List cannot be converted to ' 一维数组

excel - 如何在 Excel 中映射对象?

c# - 如何使用 C# 在 Excel 中获取用户选定范围内的特定单元格

c# - 如何将 Excel 文件插入/检索到 SQL Server 2008 中的 varbinary(max) 列?

vb.net - PayPal - 10001 内部错误 : timeout processing request Express Checkout

forms - 如何在 Excel 2016 中安装日期选择器表单控件

vba - 在一个工作簿中创建适用于另一个工作簿的子例程

c# - 使用 C# 从 Excel 中读取单元格的方法有哪些?