tags:

views:

452

answers:

3

I'm using Powershell to test a COM object method call. Due to poor design/coding/everything, this COM object method simply hangs when it errors. My default instinct is to control+c out of it, but this does not work. Do I have to kill Powershell to kill a hung COM method call?

Thanks in advance.

+6  A: 

Are you calling a method on a COM object which lives in the address space of the PowerShell process? If so then yes you must kill PowerShell in order to un-hang the call.

EDIT Matthew asked if AppDomains could be used to fix this problem.

Unfortunately no it cannot. The key here is that the COM object is executing native code. This means that at the point where the COM object hangs, the thread is in native code. There is nothing you can do from managed code to unstick a thread in native code. Even calls to .Abort() won't take effect until the thread returns to managed code.

JaredPar
Not exactly sure what you mean, but I think the answer is yes. I've instantiated the object in PS with "new-object -Com".
AJ
Is that true for the process or could he use an AppDomain for isolation?
Matthew Whited
@Matthew yes, I added a detailed reason why to my answer
JaredPar
@PITADev then most likely it is in the address space of the PS process
JaredPar
@JaredPar, thanks. It kind of sucks, but it's not Powershell's fault, of course.
AJ
+1  A: 

The easy way for me is to use pskill.

Harper Shelby
That won't work in this case. The process that is hung is PowerShell because it's making a call to an inproc COM object that's hung
JaredPar
Yeah - I saw your answer and the OP's comment after I posted.
Harper Shelby
A: 

You can try:

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($obj)

Or pipe the process to Stop-Process, adding -Force could help as well.

Shay Levy