从Visual Studio调试停止的Excel僵尸进程

时间:2022-06-04 00:37:50

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?

在应用程序调试期间(Visual Studio 2012,C#,Excel COM Interop)我通常会从Visual Studio停止应用程序(异常,或者逻辑被破坏)。这从应用程序Excel实例开始挂起在内存中。是的,我可以从任务管理器中删除它,但它非常烦人。有没有办法在Excel和我的应用程序进程之间“自定义”依赖关系?

2 个解决方案

#1


1  

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

我在我的个人资料中定义了一个Powershell函数(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.

然后我只需在Powershell提示符下键入Kill-Excel,它会杀死我机器上隐藏和可见的所有Excel实例。

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

请注意,这将删除您打开的所有Excel实例,而不提示保存。

#2


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.

使用Excel COM互操作非常棘手。首先,您需要让Excel关闭自己,但在此之前甚至可以工作,您需要确保已正确清理所有COM对象。

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.

我不确定当你关闭应用程序而没有手动杀死它时,有一种简单的方法可以关闭它,如果你不小心的话可能会关闭多个实例。我实际上建议不要在中间停止应用程序并且有一种优雅地尝试关闭excel实例的方法,这样你就可以确保它正常工作,否则一旦你部署就很有可能在野外看到这种行为。

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

这篇文章可能很有用:杀死父进程时杀死子进程

#1


1  

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

我在我的个人资料中定义了一个Powershell函数(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.

然后我只需在Powershell提示符下键入Kill-Excel,它会杀死我机器上隐藏和可见的所有Excel实例。

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

请注意,这将删除您打开的所有Excel实例,而不提示保存。

#2


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.

使用Excel COM互操作非常棘手。首先,您需要让Excel关闭自己,但在此之前甚至可以工作,您需要确保已正确清理所有COM对象。

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.

我不确定当你关闭应用程序而没有手动杀死它时,有一种简单的方法可以关闭它,如果你不小心的话可能会关闭多个实例。我实际上建议不要在中间停止应用程序并且有一种优雅地尝试关闭excel实例的方法,这样你就可以确保它正常工作,否则一旦你部署就很有可能在野外看到这种行为。

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

这篇文章可能很有用:杀死父进程时杀死子进程