Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:57:17+00:00 2026-05-14T22:57:17+00:00

I’m calling a powershell script from C#. The script is pretty small and is

  • 0

I’m calling a powershell script from C#. The script is pretty small and is “gps;$host.SetShouldExit(9)”, which list process, and then send back an exit code to be captured by the PSHost object.

The problem I have is when the pipeline has been stopped and disposed, the output reader PSHost collection still seems to be written to, and is filling up. So when I try and copy it to my own output object, it craps out with a OutOfMemoryException when I try to iterate over it. Sometimes it will except with a Collection was modified message. Here is the code.

 private void ProcessAndExecuteBlock(ScriptBlock Block)
   {
       Collection<PSObject> PSCollection = new Collection<PSObject>();
       Collection<Object> PSErrorCollection = new Collection<Object>();
       Boolean Error = false;
       int ExitCode=0;

       //Send for exection. 
       ExecuteScript(Block.Script);

       // Process the waithandles. 
       while (PExecutor.PLine.PipelineStateInfo.State  == PipelineState.Running)
       {
           // Wait for either error or data waithandle. 
           switch (WaitHandle.WaitAny(PExecutor.Hand))
           {
               // Data
               case 0:
                   Collection<PSObject> data =   PExecutor.PLine.Output.NonBlockingRead();
                   if (data.Count  > 0)
                   {
                       for (int cnt = 0; cnt <= (data.Count-1); cnt++)
                       {
                           PSCollection.Add(data[cnt]); 
                       }
                   }

                   // Check to see if the pipeline has been closed. 
                   if (PExecutor.PLine.Output.EndOfPipeline)
                   { 
                       // Bring back the exit code. 
                       ExitCode = RHost.ExitCode; 
                   }
                   break;
               case 1:
                   Collection<object> Errordata = PExecutor.PLine.Error.NonBlockingRead();
                   if (Errordata.Count > 0)
                   {
                       Error = true;
                       for (int count = 0; count <= (Errordata.Count - 1); count++)
                       {
                           PSErrorCollection.Add(Errordata[count]);
                       }
                   }
                   break;
           }
       }

       PExecutor.Stop();

       // Create the Execution Return block
       ExecutionResults ER = new ExecutionResults(Block.RuleGuid,Block.SubRuleGuid, Block.MessageIdentfier);
       ER.ExitCode = ExitCode;

       // Add in the data results.
       lock (ReadSync)
       {
           if (PSCollection.Count > 0)
           {
               ER.DataAdd(PSCollection);
           }
       }

       // Add in the error data if any.
       if (Error)
       {
           if (PSErrorCollection.Count > 0)
           {
               ER.ErrorAdd(PSErrorCollection);
           }
           else
           {
               ER.InError = true;
           }
       }

       // We have finished, so enque the block back. 
       EnQueueOutput(ER);
   }

and this is the PipelineExecutor class which setups the pipeline for execution.

public class PipelineExecutor
{
    private Pipeline pipeline;
    private WaitHandle[] Handles;

    public Pipeline PLine
    {
        get { return pipeline; }
    }

    public WaitHandle[] Hand 
    {
        get { return Handles; }
    }

    public PipelineExecutor(Runspace runSpace, string command)
    {       
        pipeline = runSpace.CreatePipeline(command);
        Handles = new WaitHandle[2];
        Handles[0] = pipeline.Output.WaitHandle;
        Handles[1] = pipeline.Error.WaitHandle;
    }

    public void Start()
    {
        if (pipeline.PipelineStateInfo.State == PipelineState.NotStarted)
        {
            pipeline.Input.Close();
            pipeline.InvokeAsync();
        }
    }

    public void Stop()
    {
        pipeline.StopAsync();
    }
}

An this is the DataAdd method, where the exception arises.

    public void DataAdd(Collection<PSObject> Data)
    {
        foreach (PSObject Ps in Data)
        {
            Data.Add(Ps);
        }
    }

I put a for loop around the Data.Add, and the Collection filled up with 600k+ so feels like the gps command is still running, but why. Any ideas.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:57:18+00:00Added an answer on May 14, 2026 at 10:57 pm

    Found the problem. Named the resultant collection and the iterator the same, so as it was iterating, it was adding to the collection, and back into the iterator, and so forth. Doh!.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 401k
  • Answers 401k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer load your "library.swf" into a new ApplicationDomain within your application… May 15, 2026 at 4:40 am
  • Editorial Team
    Editorial Team added an answer If you want RoR like environment in ASP.NET, you may… May 15, 2026 at 4:40 am
  • Editorial Team
    Editorial Team added an answer Try putting the next lines in the viewDidLoad of the… May 15, 2026 at 4:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.