Multithreading 将更改Form.Label.Text的自定义事件处理程序
我能够创建一个简单的表单,在按下按钮后显示一些文本并执行一些操作。以下是我正在使用的代码:Multithreading 将更改Form.Label.Text的自定义事件处理程序,multithreading,winforms,powershell,events,event-loop,Multithreading,Winforms,Powershell,Events,Event Loop,我能够创建一个简单的表单,在按下按钮后显示一些文本并执行一些操作。以下是我正在使用的代码: Function Button_Click() { [System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box") # Parent.Controls["Label1"].Text = "goodbye, world." } Function Generate-Form { Add-Type -A
Function Button_Click()
{
[System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
# Parent.Controls["Label1"].Text = "goodbye, world."
}
Function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# Build font object
$Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Italic)
# Build Button object
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Show Dialog Box"
#Add Button event
$Button.Add_Click({Button_Click})
# $Button.Add_Click($Button_Click)
# # Build Label object
$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Firefox status"
$Label.Name = "ffStatus"
$Label.AutoSize = $True
# Build Form object
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "My Form"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# $Form.Font = $Font
# Add button to form
$Form.Controls.Add($Button)
# Add label to form
$Form.Controls.Add($Label)
#Show the Form
$form.ShowDialog()| Out-Null
}
但现在我需要更复杂的东西。假设我想在表单标签上显示有关firefox状态的信息。我可以检查firefox是否正在运行
function Get-FirefoxStatus {
$ffRunning = 0
Get-Process| ForEach-Object { if ($_.Name -eq "firefox"){
$ffRunning = 1}
}
Return $ffRunning
}
但是如何在表单中显示Get FirefoxStatus
函数的结果。Label
我是否需要单独的线程定期调用Get FirefoxStatus
函数?或者是否有某种类型的处理程序可以注册?还是一些事件循环?我需要某种刷新按钮吗?或者该怎么做,这在powershell中是否可行?正如承诺的那样,这里的代码可以用于使用
注意,我将获取Firefox运行状态的函数更改为测试Firefox
,因此现在它返回$true
或$false
我没有更改按钮的单击功能
function Button_Click() {
[System.Windows.Forms.MessageBox]::Show("Hello World." , "My Dialog Box")
}
function Test-Firefox {
[bool](Get-Process -Name 'firefox' -ErrorAction SilentlyContinue)
}
function Generate-Form {
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# create a timer object and set the interval in milliseconds
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
# create the Tick event where the text in the label is changed
$timer.Add_Tick({
$Label.Text = if (Test-Firefox) { "Firefox is running" } else { "Firefox is not running" }
})
# Build font object
$Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Italic)
# Build Button object
$Button = New-Object System.Windows.Forms.Button
$Button.Location = New-Object System.Drawing.Size(35,35)
$Button.Size = New-Object System.Drawing.Size(120,23)
$Button.Text = "Show Dialog Box"
#Add Button event
$Button.Add_Click({Button_Click})
# # Build Label object
$Label = New-Object System.Windows.Forms.Label
$Label.Text = if (Test-Firefox) { "Firefox is running" } else { "Firefox is not running" }
$Label.Name = "ffStatus"
$Label.AutoSize = $True
# Build Form object
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "My Form"
$Form.Size = New-Object System.Drawing.Size(200,200)
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
# $Form.Font = $Font
# Add button to form
$Form.Controls.Add($Button)
# Add label to form
$Form.Controls.Add($Label)
# Stop and dispose of the timer when the form closes
$Form.Add_Closing({ $timer.Dispose() }) # Dispose() also stops the timer.
# Start the timer and Show the Form
$timer.Start()
$Form.ShowDialog()| Out-Null
# don't forget to dispose of the form when done !
$Form.Dispose()
}
# show the form
Generate-Form
您可以在表单中添加计时器,并在“勾号”事件中以指定的间隔更新标签文本。@Theo是否可以发布最小工作示例?谢谢,我现在在手机上,等我回到电脑旁就可以了。只是出于好奇。你是否知道其他能达到预期效果的技巧?@WakanTanka Well。。您可以在触发时使用各种事件来执行某些操作,例如更改标签中的文本。单击一个按钮,移动表单,按一个键,等等。这取决于你和你想要实现什么。