RunspacePool Class

Maintains a group of runspaces that have the same characteristics and can be opened and closed on an as-needed basis. Runspace pools can be used to execute multiple commands concurrently, with each command invoked in a different runspace. This class is introduced in Windows PowerShell 2.0.


Namespace: System.Management.Automation.Runspaces
Assembly: System.Management.Automation (in System.Management.Automation.dll)
'Usage
Dim instance As RunspacePool

public sealed class RunspacePool : IDisposable
public final class RunspacePool implements IDisposable
public final class RunspacePool implements IDisposable

This example shows how to create a runspace pool object that has a single runspace, open the runspace, use a PowerShell object to run commands in the runspace, how to close the runspace, and then how to dispose the runspace pool object to free resources.

using (RunspacePool rsp = RunspaceFactory.CreateRunspacePool())
{
  rsp.Open();
  
  // Create a PowerShell object to run the following command.
  //  get-process wmi*
  PowerShell gpc = PowerShell.Create();        
  // Specify the runspace to use and add commands.
  gpc.RunspacePool = rsp;
  gpc.AddCommand("Get-Process").AddArgument("wmi*");

  // Invoke the command asynchronously. 
  IAsyncResult gpcAsyncResult = gpc.BeginInvoke();
  // Get the results of running the command. 
  PSDataCollection<PSObject> gpcOutput = gpc.EndInvoke(gpcAsyncResult);

  // Process the output.
  Console.WriteLine("The output from running the command: get-process wmi*");
  for (int i= 0; i < gpcOutput.Count; i++)
  {
   Console.WriteLine(
                     "Process Name: {0} Process Id: {1}",
                     gpcOutput[i].Properties["ProcessName"].Value,
                     gpcOutput[i].Properties["Id"].Value);
  }

A runspace is an environment where commands can be run. Each runspace has a global session-state, plus session-states for any modules that are loaded in the runspace.

noteNote:
From a user’s perspective, a runspace is the session that they are currently using or a PSSession that they have created on a computer.

To create a runspace pool, call the CreateRunspacePool method of the RunspaceFactory class. You can create a pool of runspaces on the local computer or a remote computer.

This class cannot be inherited.


System.Object
  System.Management.Automation.Runspaces.RunspacePool
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

 

Target Platforms

Windows 98, Windows 2000, Windows 2000 Server, Windows CE, Windows Server 2008, Windows 98 Second Edition, Pocket PC, Smart Phone, Windows Server 2003, Windows XP Professional, Windows Vista, Windows Server 2003 R2, Windows XP, Windows 7, Windows 2008 R2, Windows Developer Preview, Windows Server Developer Preview

Send comments about this topic to Microsoft.
Show:
© 2014 Microsoft. All rights reserved.