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?