将listviewitem传递给脚本块 [英] passing a listviewitem to a script block

查看:63
本文介绍了将listviewitem传递给脚本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对PowerShell的新手,所以我现在皮肤很厚,所以你可以叫我名字: - )

A newbie to powershell so I am thick skinned at the moment and so you can call me names :-)

以下脚本评估使用背景的效率计算机上的作业。

The following script evaluates the efficiency of using background jobs on a machine.

寻找有关将listviewitem传递给scriptblock的帮助,并且无需使用"Update-TheList"。功能。任何帮助表示赞赏。我想在结束时执行的唯一操作是Remove-Job,当它更改为已完成
的状态并期望脚本块更新ListView的内容时。

Looking for help on passing the listviewitem to the scriptblock and eliminating the need for "Update-TheList" function. Any help is appreciated. The only action I want to carry out at the end is Remove-Job when it changes to a status of completed and expect the script block to update the contents of the ListView.

# Load the Winforms assembly
[void] [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms")
$ScriptBlock = {
    param ([String] $Message, [int] $seconds)
    Start-Sleep $seconds
    $date = Get-Date
    Write-Output "$Message$date"
}

Function Update-TheList ([string] $receive) {
    $split = $receive.split("=")
    $xi = New-Object System.Windows.Forms.ListViewItem
    ForEach($xi in $listView1.Items) {
        if ($xi.SubItems[0].Text -eq $split[0]) {
            $xi.SubItems[3].Text = $split[1]
            $maxEnd.Text = $split[1]
            break
        }
    }
    $listView1.refresh()
}

Function Start-TheTest {
    $date = Get-Date
    $threads = $textfield.text
    $sample  = $samplesize.text
    $listView1.Items.Clear()
    $minStart.Text = "$date"
    $totalTime.text = "0"
# iterate through the list
    for($i=0;$i -lt $sample;$i++) {
        $delay = Get-Random -Minimum 5 -Maximum 20
        $date = Get-Date
        $item = New-Object System.Windows.Forms.ListViewItem("Item $i")
        $item.SubItems.Add("$delay")        
        $item.SubItems.Add("$date")
        $item.SubItems.Add("Started")
        $listView1.Items.Add($item)
        $listView1.Refresh()
        $totalTime.Text = [string]([int]($totalTime.Text) + $delay)
        $job = Start-Job $ScriptBlock -ArgumentList "Item $i=",$delay
        While ((Get-Job -State "Running").count -ge $threads) {Start-Sleep 1}
        ForEach ($job in (Get-Job -State "Completed")) {
            $x = Receive-Job $job 
            Update-TheList $x
            Remove-Job $job
        }
    }
# Wait for all to complete
    While (Get-Job -State "Running") {
        Start-Sleep 1
        ForEach ($job in (Get-Job -State "Completed")) {
            $x = Receive-Job $job 
            Update-TheList $x
            Remove-Job $job
        }
    }
# Display output from all jobs
    ForEach ($job in (Get-Job -State "Completed")) {
        $x = Receive-Job $job 
        Update-TheList $x
        Remove-Job $job
    }
# Cleanup
    Remove-Job *
}
# Create the form
$form = New-Object Windows.Forms.Form
$form.text = "PowerShell Background Job Test"
$form.size = New-Object Drawing.Point 575,525
# Create the label control and set text, size and location
$label = New-Object Windows.Forms.Label
$label.Location = New-Object Drawing.Point 50,30
$label.Size = New-Object Drawing.Point 60,30
$label.Text = "Threads: "
$form.controls.add($label)
# Create TextBox and set text, size and location
$textfield = New-Object Windows.Forms.TextBox
$textfield.Location = New-Object Drawing.Point 115,30
$textfield.Size = New-Object Drawing.Point 30,30
$textfield.Text = 8
$form.controls.add($textfield)
# Create the label control and set text, size and location
$label = New-Object Windows.Forms.Label
$label.Location = New-Object Drawing.Point 170,30
$label.Size = New-Object Drawing.Point 60,30
$label.text = "Sample: "
$form.controls.add($label)
# Get the Sample Size
$samplesize = New-Object Windows.Forms.TextBox
$samplesize.Location = New-Object Drawing.Point 235,30
$samplesize.Size = New-Object Drawing.Point 30,30
$samplesize.Text = 20
$form.controls.add($samplesize)
# Create Button and set text and location
$button = New-Object Windows.Forms.Button
$button.text = "StartTest"
$button.Location = New-Object Drawing.Point 300,30
$button.add_click({Start-TheTest})
$form.controls.add($button)
# Create the Min Start Time label control and set text, size and location
$minStart = New-Object Windows.Forms.Label
$minStart.Location = New-Object Drawing.Point 170,65
$minStart.Size = New-Object Drawing.Point 150,30
$minStart.text = "Min Start Time"
$form.controls.add($minStart)
# Create the Max End Time control and set text, size and location
$maxEnd = New-Object Windows.Forms.Label
$maxEnd.Location = New-Object Drawing.Point 320,65
$maxEnd.Size = New-Object Drawing.Point 150,30
$maxEnd.text = "Max End Time"
$form.controls.add($maxEnd)
# Total time in seconds
$totalTime = New-Object Windows.Forms.Label
$totalTime.Location = New-Object Drawing.Point 125,65
$totalTime.Size = New-Object Drawing.Point 30,30
$totalTime.text = "0"
$form.controls.add($totalTime)
# Create the listview
$listView1 = New-Object Windows.Forms.ListView 
$listView1.Location = New-Object Drawing.Point 50,100
$listView1.Size = New-Object Drawing.Point 450,375
$listView1.Name = "listView1" 
$listView1.View = "Details"
[void] $listView1.Columns.Add("Test#")
[void] $listView1.Columns.Add("Duration")
[void] $listView1.Columns.Add("StartTime",150)
[void] $listView1.Columns.Add("EndTime",150)
$form.Controls.Add($listView1) 
# Display the dialog
$form.ShowDialog()




 


 

推荐答案

我担心您的请求过于模糊。 你不需要期望每个人都试图找出你的整个剧本。

I am afraid your request is too vague.  You will need to not expect that everyone is going to try and figure out your whole script.

这是什么不发生。

注意您无法将Forms对象发送到作业。它们不会完整地在命名空间之间传递。

Note that you cannot send Forms objects to a job. They will not pass between namespaces intact.


这篇关于将listviewitem传递给脚本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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