Invoke a pipelined command in a new runspace using the same thread #17590
Unanswered
MatejKafka
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm writing a C# cmdlet, lets call it
Invoke-InRunspace
, which invokes a command in a new runspace, to get a clean session state with no custom modules, variables,... However, I'd like to1,2,3 | Invoke-InRunspace | ...
works as if the command inside the new runspace was called directly,I believed I could do the following (just a PoC, there's missing error handling, disposal,...):
I expected this to work ok – the runspace would initialize the
Where-Object
command, add event for items added to_inputCollection
, return control back to the main runspace, which would callProcessRecord
, which would cause the command inside to process the input,...However, this deadlocks, because apparently implementation of Invoke instead starts a blocking wait on the collection, thus never returning control back to the main instance. The output works correctly without deadlocking, it's just the input that's causing the deadlock.
Is there a way to implement this, or do I have to pick between accepting pipeline input and running in a single thread?
Beta Was this translation helpful? Give feedback.
All reactions