2012-08-14 2 views
2

백그라운드에서 엑셀 좀비 프로세스를 떠나는 프로그램에 문제가 있습니다. 나는 여기에 모든 조언과 예제를 따라했지만 MSDN은 이것을 피하려고 노력하지만 그것은 내 견과를 몰고 간다. 누가 좀비 프로세스가 발생하는 원인을 발견 할 수 있습니까?(Another) .net Excel 좀비 문제

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

개인 서브 LoadExcelButton_Click (은 System.Object로 ByVal의 보낸 사람, 경우 System.EventArgs으로 ByVal의 전자)는 외부이어야한다

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

1 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 
+1

할 수 없습니다. 당신은 그것이 사용되는 의미였던 방식을 능가하지 못합니다. Excel은 데스크톱 응용 프로그램이며 interop는 이러한 단점을 보너스로 제공합니다. 이것이 MSDN이 회피하지 말 것을 권고하는 이유입니다. p – banging

+0

믿을 수 없지만 가능한 것이 틀림 없습니다. ESRI ArcGIS에서 VBA로 Excel에서 처리했습니다. VB.net에서 작업 할 수 없습니다. – sarkyscouser

답변

0

1 일을 처리 Try - End Try 및 Excel 개체를 인스턴스화하기 전에. 경로가 존재하지 않으면 코드를 정리하지 않고 하위를 종료하도록 지시하고 있습니다.

두 번째로는 GCcollect을 사용하십시오.

이것은 당신이 VB.net에서 엑셀 자동화에 촉각을 곤두 세우고있다 경우가

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) _ 
Handles Button1.Click 
     ' 
     '~~> Rest of code 
     ' 

     '~~> Close workbook and quit Excel 
     xlWb.Close (False) 
     xlApp.Quit() 

     '~~> Clean Up 
     releaseObject (xlApp) 
     releaseObject (xlWb) 
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 

나는 당신이 link 겪고 추천 할 것입니다 처리 내가 가장 좋아하는 방법입니다.

후속

사용이 코드. 대신 엑셀 응용 프로그램에 Close()를 호출

Private Sub LoadExcelButton_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Dim Name As String = "", Type As String = "", ExcelPath As String = "" 
    Dim Easting As Integer = 0, Northing As Integer = 0 
    Dim NumberSTF As Integer = 0, NumberProperties As Integer = 0, i As Integer = 0 
    Dim tDSpa As Double = 0 

    ExcelPath = Me.ExcelPathTextBox.Text.ToString 

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

    Dim xlApp As New Excel.Application 
    Dim xlBook As Excel.Workbook = Nothing 
    Dim xlSheet As Excel.Worksheet = Nothing 
    Dim STFRange As Excel.Range = Nothing 

    xlApp.Visible = True 

    xlBook = xlApp.Workbooks.Open(ExcelPath) 

    xlSheet = xlBook.Sheets("STF") 

    NumberSTF = xlSheet.UsedRange.Rows.Count - 1 
    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 

     STFRange = xlSheet.Cells(i + 1, 1) 
     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 

    '~~> Close the File 
    xlBook.Close(False) 

    '~~> Quit the Excel Application 
    xlApp.Quit() 

    '~~> Clean Up 
    releaseObject(xlSheet) 
    releaseObject(xlBook) 
    releaseObject(xlApp) 
End Sub 

'~~> Release the objects 
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 
+0

내일 사용해 보겠습니다. 감사합니다. – sarkyscouser

+0

확인해 보았으므로 차이가 없습니다. , 나는 아직도 거기에 앉아있는 좀비를 가지고있다. – sarkyscouser

+0

질문에 현재 사용중인 정확한 코드를 업데이트 할 수 있습니까? –

0

, 당신은 Dispose()를 시도?

코드의 Finally 블록에서 정리를 시도 할 수 있습니다.

+0

내가 볼 수있는 방법으로 사용할 수있는 처분 방법이 없습니다. – sarkyscouser