Powershell 计时器 - 更新 gui [英] Powershell Timer - Update gui

查看:54
本文介绍了Powershell 计时器 - 更新 gui的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 powershell 计时器,每 1 秒后,我想运行一个函数来执行或发布文本到界面上的文本框日志.

I have created a powershell timer, and after every 1 second, I would like to run a function to execute or post text to a text box log on the interface.

运行下面的.当您单击开始时,每隔 1 秒,日志文本区域应显示每 1 秒发布一次日志,而不是批量".但是,当您单击停止"时,这些消息只会一次性显示为一批.

Run the below. When you click Start, every 1 second, the log text area should show "Post to log every 1 second, not as a batch". However, this messages only appear as a batch, all at once, when you click Stop.

这个问题网上好像没有答案!

This question does not appear to be answered on the internet!

代码:

$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()

感谢您的高级帮助.

-R

推荐答案

我拿了你的东西,看着这个 egg_timer 项目并想出了以下内容:

I took what you had, and looked at this egg_timer project and came up with the following:

  $timer = New-Object System.Windows.Forms.Timer
  $timer.Interval = 1000
  $timer.add_tick({AddToLog 'Post to log every 1 second, not as a batch'})



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

function startTimer() { 

   $timer.start()

}

function stopTimer() {

    $timer.Enabled = $false
    Write-Host "Close Function"

}


########################
# 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()

这篇关于Powershell 计时器 - 更新 gui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆