Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RunspaceFactory.CreateRunspacePool Method ()

 

Updated: April 27, 2016

Applies To: Windows PowerShell

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)

public static RunspacePool CreateRunspacePool()

Return Value

Type: System.Management.Automation.Runspaces.RunspacePool

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.

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.

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.

namespace HostRunspacePool
{
  using System;
  using System.Collections.ObjectModel;
  using System.Management.Automation;
  using System.Management.Automation.Runspaces;

  internal class HostRunspacePool
  {

    private static void Main(string[] args)
    {
      // Create a pool of runspaces.
      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);
        }
      } // End using.
    } // End Main entry point.
  } // End HostPs5 class.
}

Return to top

Community Additions

Show: