RunspaceFactory.CreateRunspacePool Method ()

Creates a runspace pool that contains a single runspace. This runspace uses the default host and default runspace configuration. This method is introduced in Windows PowerShell 2.0.


Namespace: System.Management.Automation.Runspaces
Assembly: System.Management.Automation (in System.Management.Automation.dll)

'Usage
Dim returnValue As RunspacePool

returnValue = RunspaceFactory.CreateRunspacePool

public static RunspacePool CreateRunspacePool ()
public static RunspacePool CreateRunspacePool ()
public static function CreateRunspacePool () : RunspacePool

Return Value

Returns a RunspacePool object that can be used to open and close a single runspace. When opened, the runspace uses the default host and runspace configuration provided by Windows PowerShell.

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 pool is typically a set of runspaces that have the same features, such as the same host, initial session state, and connection information. A runspace from that pool is the operating environment used when invoking the commands of a pipeline. After the runspace pool is created, a runspace from the pool must be opened by using one of the following methods before it can be used:

  • To open a runspace from the pool synchronously, call the Open method.

  • To open a runspace from the pool asynchronously, call the BeginOpen method.


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