Excel实例没有在任务pipe理器VB.net中被杀死
一旦button执行结束,任务pipe理器中的Excel实例(excel.exe)不会被杀死。
Private Sub Extract_Click(sender As Object, e As EventArgs) Handles Extract.Click Dim xlApp As New Excel.Application Dim wbXLsource As Excel.Workbook Dim wbXLtarget As Excel.Workbook Dim shXLsource As Excel.Worksheet Dim shXLtarget As Excel.Worksheet Dim datestart As Date = Date.Now wbXLsource = xlApp.Workbooks.Open(xlSourcePath) wbXLtarget = xlApp.Workbooks.Open(xlTargetPath) Dim dateEnd As Date = Date.Now End_Excel_App(datestart, dateEnd) End Sub Private Sub End_Excel_App(datestart As Date, dateEnd As Date) Dim xlp() As Process = Process.GetProcessesByName("EXCEL") For Each Process As Process In xlp If Process.StartTime >= datestart And Process.StartTime <= dateEnd Then Process.Kill() Exit For End If Next End Sub
我也尝试使用垃圾回收器的概念,但Excel应用程序closures后才被杀死。 我需要在button执行结束后closuresexcel实例(excel.exe)
使用这个代码….我一直在使用这个,它每次都closures
For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL") If proc.MainWindowTitle.Trim() = "" Then proc.Kill() End If Next
如果您有Try Catcherror handling程序…
Try 'stuff more stuff Catch Ex as Exception For Each Proc in System....... ...... Next Finally GC.Collect() End Try
每次工作。
通过调用xlApp.Quit()
closures应用程序将closures它,但是如果从内存中不会被删除。
为了也释放记忆,你可以按照Siddharth Rout的出色答案给出的build议:
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