WinForm Label Status Update RRS feed

  • Question

  • Good morning Everyone,

                 So, I am trying to build a self-help tool for my company. It is going pretty well, thanks for asking. Anyway, my boss comes by and makes a request to have the windows form update the status of the processes it is doing at the time. I have intermediate ability with PowerShell, but I thought easy right? WRONG! Let me show you what I have.

    $PingLabel = New-Object Windows.Forms.Label
    $PingLabel.Text = "Begin Testing Network Connections"
    $PingLabel.AutoSize = $true
    $PingForm.StartPosition = "CenterScreen"
    While(test-netconnection localhost){$PingLabel.Text = "Pinging Local Host"}


    So, to me, it seems like this should work. While it is pinging the local host, the text of the label is "Pinging Local Host". But, it just sits there when I run it. Can you change the text of a label while a command is running? I tried googling it, but I don't think I am phrasing it right. Any help, as always, will be very appreciated.  

    Tuesday, February 28, 2017 5:13 PM


All replies

  • No.  The code that is not in an event will not run or, as posted, will block until you cancel it.

    Place a button on the form and use it to ping once and set the text.  To do something repeatedly you will need to use a timer control.


    Tuesday, February 28, 2017 6:43 PM
  • Thanks for your reply. I don't want the user to interface with this form. The previous window will ask them to save work before hitting the continue button, and then goes into a series of tasks. I just want to update them while it is running through a series of pings, i.e. Localhost, gateway, DC, Google

    I will look up timer controls and try to see if there is a solution in there.

    Tuesday, February 28, 2017 6:51 PM
  • You cannot do this from outside of a form event. 


    Tuesday, February 28, 2017 6:58 PM
  • I am realizing that, so I guess the solution is to create a window for each task and then to close it when the task is complete. How does that sound? Or am I missing something?
    Tuesday, February 28, 2017 7:33 PM
  • Run the tasks as jobs and poll the jobs with a timer to retrieve the status.  All of this can be done in one form.

    With PowerShell you can have only one active form at a time.


    Tuesday, February 28, 2017 8:05 PM
  • Could you give me an example of how to do that, please? I am reading about it now, but it would be helpful to see it from someone else.
    Tuesday, February 28, 2017 9:32 PM
    • Marked as answer by RedBeardOne Tuesday, March 7, 2017 7:57 AM
    Tuesday, February 28, 2017 9:35 PM
  • There should be plenty of examples available by searching.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, February 28, 2017 9:35 PM