How to run two cmdlets in parallel in the same Runspace. I am using C #.
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.AuthorizationManager = new AuthorizationManager("MyShellId");
iss.ImportPSModule(new string[] { "MSOnline" });
Runspace powerShellRunspace = RunspaceFactory.CreateRunspace(iss);
In two threads, I run cmdlets using the same workspace.
Pipeline pipeLine = powerShellRunspace.CreatePipeline();
pipeLine.Commands.Add(shellCommand);
pipeLine.Input.Close();
pipeLine.Invoke();
pipeLine.Output.DataReady += new EventHandler(processData); //processData is a method which processes data emitted by pipeline as and when it comes to avoid out of memory
if (pipeLine.Error != null && pipeLine.Error.Count > 0) {
Collection<Object> errors = (Collection<Object>)(pipeLine.Error.ReadToEnd());
//process those errors
}
But when two threads simultaneously use the same space to run cmdlets. I get an exception: "Pipeline is not running because the pipeline is already running. Conveyors cannot run at the same time."
I need to use the same workspace for performance reasons. How to achieve my goal?
source
share