Powershell Timer - atualização de gui

Eu criei um cronômetro PowerShell, e após cada 1 segundo, gostaria de executar uma função para executar ou postar texto em um log de caixa de texto na interface.

Execute o abaixo. Quando você clica em Iniciar, a cada 1 segundo, a área de texto do log deve mostrar "Postar para registrar a cada 1 segundo, não como um lote". No entanto, essas mensagens só aparecem como um lote, tudo de uma vez, quando você clica em Parar.

Esta pergunta não parece ser respondida na internet!

Código:

$global:timer = New-Object System.Timers.Timer
$global:timer.Interval = 1000


function AddToLog($logtext)
{
    $txtLog.Text = $txtLog.Text + "`r`n" + $logtext
    $txtLog.ScrolltoCaret
}

function startTimer() { 
    Register-ObjectEvent -InputObject $global:timer -EventName Elapsed -SourceIdentifier theTimer -Action {AddToLog('Post to log every 1 second, not as a batch') }
    $global:timer.start()
    Write-Host "Start Timer"
}

function stopTimer() {
    $global:timer.stop()
    Write-Host "Close Function"
    Unregister-Event theTimer
}


########################
# Setup User Interface
########################

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Timer Example"
$objForm.Size = New-Object System.Drawing.Size(330,380) 
$objForm.StartPosition = "CenterScreen"


#Start Button
$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Location = New-Object System.Drawing.Size(10,190)
$btnStart.Size = New-Object System.Drawing.Size(140,35)
$btnStart.Text = "Start"

$btnStart.Add_Click({StartTimer; })
$objForm.Controls.Add($btnStart)

#Stop Button
$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Location = New-Object System.Drawing.Size(150,190)
$btnStop.Size = New-Object System.Drawing.Size(140,35)
$btnStop.Text = "Stop"
$btnStop.Add_Click({StopTimer; })
$objForm.Controls.Add($btnStop)
$btnStop.Enabled  = $true

#Log Area
$lblLog = New-Object System.Windows.Forms.Label
$lblLog.Location = New-Object System.Drawing.Size(10,230) 
$lblLog.Size = New-Object System.Drawing.Size(80,20) 
$lblLog.Text = "Event Log:"
$objForm.Controls.Add($lblLog) 

$txtLog = New-Object System.Windows.Forms.Textbox
$txtLog.Location = New-Object System.Drawing.Size(10,250)
$txtLog.Size = New-Object System.Drawing.Size(290,90)
$txtLog.Multiline = $True
$txtLog.Scrollbars = "vertical"

$txtLog.Add_Click({$txtLog.SelectAll(); $txtLog.Copy()})
$objForm.Controls.Add($txtLog)

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

Obrigado pela sua ajuda em avançado.

-R

questionAnswers(2)

yourAnswerToTheQuestion