使用powershell.Create创建线程,addArgument传递参数- cls
- [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
-
- $job1 = {
- param($btn,$counter)
- $counter..0 | foreach {
- $btn.Text = $_
- Start-Sleep -Milliseconds 100
- }
- }
-
- $f = New-Object "System.Windows.Forms.Form"
- $f.Width = 210
- $f.Height = 200
- $f.StartPosition = 'CenterScreen'
-
- $b = New-Object 'System.Windows.Forms.Button'
- $b.Location = New-Object 'System.Drawing.Point'(50,50)
- $b.Width = 100
- $b.Height = 50
- $b.Text = 'Click'
- $b.add_Click({
- [powershell]::Create().
- AddScript($job1). #脚本
- AddArgument($this). #参数1 btn
- AddArgument(200). #参数2 counter
- BeginInvoke()
- })
- $f.Controls.Add($b)
-
- $f.ShowDialog()
复制代码
|