Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell计时器-更新gui_Powershell_User Interface_Timer - Fatal编程技术网

Powershell计时器-更新gui

Powershell计时器-更新gui,powershell,user-interface,timer,Powershell,User Interface,Timer,我已经创建了一个powershell计时器,每隔1秒,我想运行一个函数来执行或将文本发布到界面上的文本框日志中 运行下面的命令。单击“开始”时,每隔1秒,日志文本区域应显示“每1秒发布日志,而不是批量发布”。但是,当您单击“停止”时,这些消息仅作为一个批同时显示 这个问题在互联网上似乎没有得到回答 代码: 感谢您在高级课程中的帮助 -R尝试使用System.Windows.Forms.Timer。看 对于WinForms计时器,在调用ShowDialog()之前,请放弃Register Obje

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

运行下面的命令。单击“开始”时,每隔1秒,日志文本区域应显示“每1秒发布日志,而不是批量发布”。但是,当您单击“停止”时,这些消息仅作为一个批同时显示

这个问题在互联网上似乎没有得到回答

代码:

感谢您在高级课程中的帮助


-R

尝试使用
System.Windows.Forms.Timer
。看

对于WinForms计时器,在调用ShowDialog()之前,请放弃Register ObjectEvent和Unregister事件并添加此行:

不要忘记更改为System.Windows.Forms.Timer。当我进行这些更改时,UI工作正常


使用Register ObjectEvent要求PowerShell在事件队列执行的各个安全点为其提供服务。但是,由于PowerShell在某些.NET代码(Form.ShowDialog())中被阻止,因此它没有机会为其事件队列提供服务。

我接受了您所拥有的,并查看了此项目,得出以下结论:

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

我复制粘贴了代码,单击“停止”时在GUI中没有收到任何消息。你能重新检查一下你的代码吗?@Vish我想你把代码粘贴到了一个脚本文件中,然后运行了脚本?尝试将代码粘贴到控制台窗口中,然后按照Rya所述工作。嗨,Keith,虽然System.Windows.Forms.Timer可以工作,但我似乎找不到System.Windows.Forms.Timer写入主机的基本示例,假设每2秒“仍在运行”,直到程序显示停止运行。甚至可以登录界面,就像我的例子一样…谢谢;这对我有效,而我发现的所有其他答案都阻止了你提到的线程。谢谢你的示例代码。这很有效。解决了我的问题。为了让这个脚本正常工作,我必须将前三行(
$timer
$timer.Interval
$timer.add_tick
)移到
AddToLog
的正下方。我还建议添加
$objForm.add_Closing({StopTimer;})
$timer.add_Tick({AddToLog('Post to log every 1 second, not as a batch')})
  $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()