Can Pipelines run concurrently in the same Runspace?

Question:

How to run two cmdlets parallely in same Runspace. I am using C#.

In two threads, i will run cmdlets using the same runspace.

But when two threads simultaneously using the same runspace to run cmdlets. I am getting exception, “Pipeline not executed because a pipeline is already executing. Pipelines cannot be executed concurrently.”

I need to use same runspace for performance reasons. How to achieve my objective?

Answer:

Have you looked at the System.Management.Automation.Runspaces.RunspacePool class? Using it and the InitialSessionState can assist with eliminating overhead from module imports as it is only done once per pool and not per runspace. If you are looking for asynchronous execution of powershell commands here is a very basic, not production-ready example: (note I am not at a computer with Visual Studio but this should be right)


This should give:

Source:

Can Pipelines run concurrently in the same Runspace? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply