3

During app debugging (Visual Studio 2012, C#, Excel COM Interop) I usually stop the app from Visual Studio (exception, or the logic is broken). This leaves started from the app Excel instance to hang in the memory. Yes, I can kill it from the Task Manager, but it is pretty annoying. Is there any way to "customize" dependency between Excel and my app process?

Denis Gladkiy
  • 2,084
  • 1
  • 26
  • 40
  • Have a go at this, maybe this will help you cleanup properly http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – Mo Patel May 30 '14 at 09:09
  • This is normal. By terminating your app early, before the normal cleanup can happen, Excel doesn't get notified that the COM interface pointers are no longer in use. A general liability of process interop. The normal backstop that COM implements, garbage collecting stale interface pointers after a timeout, is not functional for Office apps. No idea why. Keep Task Manager handy, you don't have to kill immediately, or write a little utility that uses Process.Kill(). – Hans Passant May 30 '14 at 10:31

2 Answers2

1

I have a Powershell function defined in my profile (C:\Users\{userName}\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1)

function Kill-Excel {
    $excelInstances = @(Get-Process | Where { $_.Name -Eq "Excel" }).Count
    if ($excelInstances -Eq 0) {
        "Excel not running"
    }
    else {
        if ($excelInstances -Eq 1) {
            "Killing 1 instance of Excel"
        }
        else {
            "Killing $excelInstances instances of Excel"
        }
        Get-Process | Where { $_.Name -Eq "Excel" } | Kill
    }
}

Then I just type Kill-Excel in a Powershell prompt which kills all Excel instances, hidden and visible, on my machine.

Note that this will take down any Excel instances you have open without prompting to save.

Patrick McDonald
  • 64,141
  • 14
  • 108
  • 120
  • 1
    Nice script. There is an issue: among zombies I have one live Excel instance. Anyway this will be faster than Task Manager UI. – Denis Gladkiy May 30 '14 at 11:19
  • I don't think this is an elegant solution. It is more like, "What do I do, I have mosquitoes in my house? No problem, gas your house with a mixture of DDT, Hydrogen Cyanide, Formaldehyde and Sarin, and then blow the house up with RDX." – Shakti Prakash Singh Jun 02 '14 at 07:02
  • 1
    Elegant it's not. It may not blow up the house, but it does kill everything in it. It works for me, when I just want to exterminate the house's occupants with minimal killing fatigue. – Patrick McDonald Jun 02 '14 at 13:30
0

Using the Excel COM interop is quite tricky. Firstly you'll need to ask Excel to close itself but before that will even work you need to make sure that you've properly cleaned up all your COM objects.

I'm not sure there's an easy way to make it close when you're application shuts down without manually killing it, which could take down multiple instances if you're not careful. I'd actually recommend not stopping the app in the middle and have a way of gracefully trying to shutdown the excel instances so you can ensure it's working, otherwise there's a high chance that you might see this behaviour in the wild once you've deployed.

This post may be useful: Kill child process when parent process is killed

Community
  • 1
  • 1
Ian
  • 33,605
  • 26
  • 118
  • 198