为什么在任务管理器中,当我关闭 Form1 时 Excel 进程不存在,但 Outlook 进程存在?
Why does, within Task Manager, the Excel process not exist when I close Form1 but the Outlook process does?
运行 以下代码并检查您的任务管理器以查看 Excel 和 Outlook 进程是否存在:
Imports Microsoft.Office.Interop
Public Class Form1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Kill all EXCEL.EXE's from Task Manager
For Each prog As Process In Process.GetProcessesByName("EXCEL")
prog.Kill()
Next
'Kill all OUTLOOK.EXE's from Task Manager
For Each prog As Process In Process.GetProcessesByName("OUTLOOK")
prog.Kill()
Next
'Open new EXCEL.EXE in the Task Manager
Dim xlApp As New Excel.Application
'Open new OUTLOOK.EXE in the Task Manager
Dim olApp As New Outlook.Application
End Sub
End Class
关闭Form1
检查您的任务管理器,发现 Excel 进程不存在,但 Outlook 进程存在。
为什么在任务管理器中,当我关闭 Form1 时 Excel 进程不存在而 Outlook 进程存在?
尝试这样开始 Excel:
Dim xlApp As New Process
xlApp.StartInfo.FileName = "filePath/excel.exe"
xlApp.Start
如果这没有帮助,只需尝试启动一个 exe:
Process.Start("filePath\excel.exe")
我无法重现您遇到的这个问题。 Outlook 进程确实需要更长的时间才能结束,但它 确实 结束了。
也就是说,有 更好 的方法来关闭这些进程。正如我在评论中所说,看看这个 which links to Siddharth Rout's bit of code.
首先,您需要在 class 级别声明 xlApp
和 olApp
。您还需要将以下方法添加到您的代码中:
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
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
然后您可以在关闭表单时调用此方法:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
xlApp.Quit()
ReleaseObject(xlApp)
olApp.Quit()
ReleaseObject(olApp)
End Sub
总体而言,您的代码看起来类似于:
Public Class Form1
Private xlApp As Excel.Application
Private olApp As Outlook.Application
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Open new EXCEL.EXE in the Task Manager
xlApp = New Excel.Application
'Open new OUTLOOK.EXE in the Task Manager
olApp = New Outlook.Application
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
xlApp.Quit()
ReleaseObject(xlApp)
olApp.Quit()
ReleaseObject(olApp)
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
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class
运行 以下代码并检查您的任务管理器以查看 Excel 和 Outlook 进程是否存在:
Imports Microsoft.Office.Interop Public Class Form1 Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load 'Kill all EXCEL.EXE's from Task Manager For Each prog As Process In Process.GetProcessesByName("EXCEL") prog.Kill() Next 'Kill all OUTLOOK.EXE's from Task Manager For Each prog As Process In Process.GetProcessesByName("OUTLOOK") prog.Kill() Next 'Open new EXCEL.EXE in the Task Manager Dim xlApp As New Excel.Application 'Open new OUTLOOK.EXE in the Task Manager Dim olApp As New Outlook.Application End Sub End Class
关闭
Form1
检查您的任务管理器,发现 Excel 进程不存在,但 Outlook 进程存在。
为什么在任务管理器中,当我关闭 Form1 时 Excel 进程不存在而 Outlook 进程存在?
尝试这样开始 Excel:
Dim xlApp As New Process
xlApp.StartInfo.FileName = "filePath/excel.exe"
xlApp.Start
如果这没有帮助,只需尝试启动一个 exe:
Process.Start("filePath\excel.exe")
我无法重现您遇到的这个问题。 Outlook 进程确实需要更长的时间才能结束,但它 确实 结束了。
也就是说,有 更好 的方法来关闭这些进程。正如我在评论中所说,看看这个
首先,您需要在 class 级别声明 xlApp
和 olApp
。您还需要将以下方法添加到您的代码中:
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
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
然后您可以在关闭表单时调用此方法:
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
xlApp.Quit()
ReleaseObject(xlApp)
olApp.Quit()
ReleaseObject(olApp)
End Sub
总体而言,您的代码看起来类似于:
Public Class Form1
Private xlApp As Excel.Application
Private olApp As Outlook.Application
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
'Open new EXCEL.EXE in the Task Manager
xlApp = New Excel.Application
'Open new OUTLOOK.EXE in the Task Manager
olApp = New Outlook.Application
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
xlApp.Quit()
ReleaseObject(xlApp)
olApp.Quit()
ReleaseObject(olApp)
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
obj = Nothing
Catch ex As Exception
obj = Nothing
Finally
GC.Collect()
End Try
End Sub
End Class