2

Excel instance (excel.exe) in task manager is not getting killed once button's execution is over.

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

I tried using garbage collector concept also but excel instance is getting killed only after closing of app. I need to close the excel instance (excel.exe) after button execution gets over

Gurpreet
  • 37
  • 5
  • 1
    If memory serves correctly, there's a `close` method on an excel workbook that would bypass this hackery, and would correctly close the document (and excel instance) upon being called. – user2366842 Feb 20 '17 at 18:54
  • You start the app before you grab the start time..... Date time is a LOT more granular than you think. But as @user2366842 states.. there is a method to do this properly. – Trevor_G Feb 20 '17 at 18:54

2 Answers2

0

Closing the application by calling xlApp.Quit() closes it but won't remove if from memory.

To also free the memory, you can follow the advice given by Siddharth Rout's excellent answer:

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
Community
  • 1
  • 1
Leviathan
  • 2,468
  • 1
  • 18
  • 24
  • Calling `xlApp.Quit()` closes the excel but it doesn't kill the excel instance (excel.exe) in the task manager – Gurpreet Feb 21 '17 at 06:41
  • You are right, I messed up my testing. I found another answer on this topic on SO and updated my answer to link to it. – Leviathan Feb 21 '17 at 08:15
  • Same issue persisits with this one also. excel.exe instance still remains in the task manager even after button execution is over – Gurpreet Feb 23 '17 at 11:33
0

Use this code....I've been using this and it closes every time

For Each proc In System.Diagnostics.Process.GetProcessesByName("EXCEL")
   If proc.MainWindowTitle.Trim() = "" Then
         proc.Kill()
   End If
Next

And if you have a Try Catch error handler...

Try

     'stuff more stuff

Catch Ex as Exception 

      For Each Proc in System.......
      ......
      Next
Finally
       GC.Collect()
End Try

Works every time.

BobSki
  • 1,531
  • 2
  • 25
  • 61
  • 2
    or just `If proc.MainWindowHandle = IntPtr.Zero Then` – Slai Feb 20 '17 at 19:37
  • Thanks BobSki. This code works fine when no other excel is opened. But in case if some other excel (apart from the ones called in VB.net) is opened, the code doesn't work. Can you suggest something for that. – Gurpreet Feb 21 '17 at 05:37
  • Ahh you are correct, why don't you try @Slai 's suggestion? – BobSki Feb 22 '17 at 13:53
  • @Gurpreet `.MainWindowTitle` is `null` when the process doesn't have a main window, so `null.Trim` would result in Exception. If I remember correctly, it works only on processes that don;t have main window and are not currently used or referenced by your application – Slai Feb 22 '17 at 14:04
  • Yes it works the same way @Slai. Thanks for the clarification. – Gurpreet Feb 23 '17 at 05:19