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 8710131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:32:43+00:00 2026-06-13T04:32:43+00:00

I have a powershell cmdlet written in C# (deriving from PSCmdlet ) which will

  • 0

I have a powershell cmdlet written in C# (deriving from PSCmdlet) which will start a long-running task which should update its progress using WriteProgress() while it is running. Since powershell will not allow a separate thread to use WriteObject or WriteProgress I had to create a Queue<object> in the main thread and I add items to the queue from the task that I want to be written to the Pipeline/Progress. A while loop will dequeue objects as they come in and write to the pipline / progress bar.

This is working, but I wanted to see if there were any better practices for multi-threading with a powershell cmdlet that is written in C#/VB. For example with WPF I can always step onto the UI thread with UIComponent.Dispatcher.Invoke() if I need to update a progress bar or UI Component. Is there anything equivalent that I can use to ‘step onto’ the powershell thread to update the UI or write to the pipeline?

  • 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-06-13T04:32:45+00:00Added an answer on June 13, 2026 at 4:32 am

    Here is an example of the queue system encapsulated in a class so it is easier to use and mimics Cmdllet.WriteObject’s behavior. This way you can call WriteObject from within the separate thread and the object will be marshalled onto the powershell thread and written to the pipeline.

    [Cmdlet("Test", "Adapter")]
    public class TestCmdlet : PSCmdlet
    {
        protected override void ProcessRecord()
        {
            PowerShellAdapter adapter = new PowerShellAdapter(this, 100);
            Task.Factory.StartNew(() => {
                for (int x = 0; x < 100; x++) {
                    adapter.WriteObject(x);
                    Thread.Sleep(100);
                }
                adapter.Finished = true;
            });
            adapter.Listen();
        }
    }   
    

    public class PowerShellAdapter
    {
        private Cmdlet Cmdlet { get; set; }
        private Queue<object> Queue { get; set; }
        private object LockToken { get; set; }
        public bool Finished { get; set; }
        public int Total { get; set; }
        public int Count { get; set; }
    
        public PowerShellAdapter(Cmdlet cmdlet, int total)
        {
            this.Cmdlet = cmdlet;
            this.LockToken = new object();
            this.Queue = new Queue<object>();
            this.Finished = false;
            this.Total = total;
        }
    
        public void Listen()
        {
            ProgressRecord progress = new ProgressRecord(1, "Counting to 100", " ");
            while (!Finished || Queue.Count > 0)
            {
                while (Queue.Count > 0)
                {
                    progress.PercentComplete = ++Count*100 / Total;
                    progress.StatusDescription = Count + "/" + Total;
                    Cmdlet.WriteObject(Queue.Dequeue());
                    Cmdlet.WriteProgress(progress);
                }
    
                Thread.Sleep(100);
            }
        }
    
        public void WriteObject(object obj)
        {
            lock (LockToken)
                Queue.Enqueue(obj);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PowerShell script that calls a CmdLet which in turn reports its
I have 2 powershell scripts that I execute from c# which I'm using to
I have a custom C# PowerShell Cmdlet (inheriting from the Cmdlet base class) and
Does PowerShell have an equivalent to this command construct from sh (and derivatives): $
I have a powershell script that will generate an email for users whose password
I have a powershell command running when I execute a bat file, The command
What's the best way to get the current PowerShell Cmdlet from another object? If
In PowerShell you can use the Get-WmiObject cmdlet to grab WMI classes. I have
I have a PowerShell script cmdlet that supports the -WhatIf & -Confirm parameters. It
I have a powershell script that needs to read from a config file with

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.