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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:29:27+00:00 2026-06-17T12:29:27+00:00

EDIT: The sample code is far off from the original goal, but there’s a

  • 0

EDIT: The sample code is far off from the original goal, but there’s a legitimate answer to the question that I actually posed, so I’ve edited the question and title.

I am writing a multithread PowerShell script with a WinForms GUI and would like to update the GUI from one of the other threads in the script. I’m having no luck doing this at all. In fact, I can’t even get BeginInvoke to work.

Here’s a very stripped down example. This is sample code, not production, so it’s not pretty, but it’s replicating the issue:

$script:SynchronizedUIHash = [Hashtable]::Synchronized(@{});

[reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
$form= New-Object Windows.Forms.Form
$label = New-Object Windows.Forms.Label
$label.Text = 'original'
$script:SynchronizedUIHash.label = $label

$form.Controls.add($label)
$form.show()
[System.Windows.Forms.Application]::DoEvents()

Read-Host

$label.BeginInvoke(
    [Action[string]] {
        param($Message)
        [Console]::Beep(500,300)
        $l = $SynchronizedUIHash.label
        $l.Text = $Message
        [System.Windows.Forms.Application]::DoEvents()
    },
    'changed'
)

Read-Host

$form.close()

I don’t even hear the console beep, so it’s as if the BeginInvoke code isn’t being executed at all.

If I throw in a [System.Windows.Forms.Application]::DoEvents() after the Read-Host then I see the change so it seems to be a timing issue, but I don’t want to go into a spin loop or something silly like that – I want to fire and forget the BeginInvoke and have the results visible as appropriate. I see other answers like the one to PowerShell: Job Event Action with Form not executed but that needs to be in a check, sleep, check loop.

There’s got to be a simpler way that I’m missing… what is it?

What is the right solution here?

EDIT: Keith’s comment made me realize I missed a key point going from production to the stripped sample code – launching a job is indeed multi-process, but the sample code isn’t, so the sample doesn’t reflect real life, and I’ll have to rethink the whole thing. That all said – the sample code doesn’t use jobs, and it doesn’t work as I expect either, so I’d like to understand why, even if that doesn’t help me in this specific case.

  • 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-17T12:29:28+00:00Added an answer on June 17, 2026 at 12:29 pm

    You can’t really have a Console and a Windows run on the same thread. I suggest you create another thread to host the window, like this (It’s a C# console app, but it should be easy to translate):

    class Program
    {
        static void Main(string[] args)
        {
            Form form = new Form();
            Label label = new Label();
            label.Text = "original";
            form.Controls.Add(label);
    
            // run this form on its own thread, so it can work properly
            Thread t = new Thread((state) => Application.Run((Form)state));
            t.Start(form);
    
            Console.ReadLine();
    
            // note you *could* do label.Text = "other" here, but BeginInvoke is always recommended
            label.BeginInvoke((Action)delegate() { label.Text = "other"; });
    
            Console.ReadLine();
            Application.Exit();
        }
    }
    

    EDIT: It took me a while to cook a Powershell version, but here it is. The trick is: you just can’t create a standard .NET thread from the Powershell environement. Period. As soon as you try to do it, you will crash Powershell. The underlying error that causes the crash is this:

    System.Management.Automation.PSInvalidOperationException: There is no
    Runspace available to run scripts in this thread. You can provide one
    in the DefaultRunspace property of the
    System.Management.Automation.Runspaces.Runspace type

    And, you can’t really try to set this DefaultRunspace variable from another thread because it’s marked threadstatic (one copy per thread)! Now, if you google for multi-threading Powershell, you will find many articles about Powershell Jobs. But these allow multi-tasking, but it’s not really multi-threading, it’s more heavyweight.

    So… turns out the solution is really to use Powershell features, that is: Powershell runspace. Here it is:

    [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
    $form = New-Object System.Windows.Forms.Form
    $label = New-Object System.Windows.Forms.Label
    $label.Text = 'original'
    $form.Controls.add($label)
    
    $ps = [powershell]::create()
    $ps.AddScript(
         {
         [System.Windows.Forms.Application]::Run($form)
         })
    $ps.Runspace.SessionStateProxy.SetVariable("form", $form)
    $ps.BeginInvoke()
    
    Read-Host
    
    $label.BeginInvoke(
        [Action[string]] {
            param($Message)
            $label.Text = $Message
        },
        'changed'
    )
    
    Read-Host
    
    [System.Windows.Forms.Application]::Exit()
    $ps.Dispose()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a simple way to reformat my HTML from within Komodo Edit or
Check out this code sample of a button and an anchor: http://jsbin.com/ecitex/2/edit I'm trying
EDIT: Simple version of the question: I want to create server variables in the
Edit (updated question) I have a simple C program: // it is not important
Just wondering why almost every controller method I see in sample MVC code returns
Edit: For simplicity, and in order to try and make this question and the
I'm converting some code from ASM to C++, the ASM simply looks like so:
here's the sample code (fully working - just copy to an empty html file
Trying to update some old code from live() to use on() and something is
Once again I found JavaScript code on the Internet that contains an inline function

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.