Powershell计时器没有暂停表格

时间:2013-01-31 18:13:11

标签: timer powershell-v2.0

美好的一天

像往常一样,我被卡住了,我有一个简单的脚本,旨在向您展示系统列表锁定器的状态。给它一个系统名称的txt,它完成剩下的工作。一切按预期工作;然而,它在滴答计时器上更新列表,在执行时会使窗口无响应并且看起来被破坏(对那些不了解它在做什么的用户)。有没有办法以某种方式解决这个问题以避免这种问题?

我考虑过做一个分支,但我现在知道如何让该分支更新其父对象...如果可能的话。

CODE:

[void] [Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' ) 
$d = New-Object Windows.Forms.OpenFileDialog 
$d.ShowHelp = $true 
$d.filter = "System ID List (*.txt)| *.txt"
$result = $d.ShowDialog( )
$names = @()
$names = Get-Content $d.filename

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")

$myWindow = new-object System.Windows.Forms.form
$myDataGrid = new-object System.windows.forms.DataGridView
$myDataGrid.Location = new-object System.Drawing.Size(20,30)
$myDataGrid.size = new-object System.Drawing.Size(450,480)
$myDataGrid.AllowUserToAddRows = $False
$myDataGrid.AutoSizeColumnsMode = [System.Windows.Forms.DataGridViewAutoSizeColumnsMode]::Fill
$myDataGrid.RowsDefaultCellStyle.BackColor = [System.Drawing.Color]::Bisque
$myDataGrid.AlternatingRowsDefaultCellStyle.BackColor = [System.Drawing.Color]::Beige
$myDataGrid.BorderStyle = [System.Windows.Forms.BorderStyle]::Fixed3D
$myDataGrid.ColumnHeadersDefaultCellSTyle.ForeColor = [System.Drawing.Color]::Maroon
$myDataGrid.ColumnHeadersDefaultCellStyle.BackColor = [System.Drawing.Color]::Tan
$myDataGrid.RowHeadersDefaultCellStyle.BackColor = [System.Drawing.Color]::Tan
$myDataGrid.ColumnHeadersHeightSizeMode = [System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode]::AutoSize
$myWindow.Controls.Add($myDataGrid)


# Define menus
$myMenuStrip = new-object System.Windows.Forms.MenuStrip
$FileExit = new-object System.Windows.Forms.ToolStripMenuItem("&Exit")
$FileExit.add_Click({ $myWindow.close() })
$myMenuStrip.Items.Add($FileMenu)
$myWindow.Controls.Add($myMenuStrip)

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000
$timer.add_tick({
    $dataTable = New-Object System.Data.DataTable            
    $dataTable.Columns.Add("System")  | Out-Null
    $dataTable.Columns.Add("BitLocker % (C:)")   | Out-Null        
    foreach ($name in $names) {
        $stat = (manage-bde.exe -cn $name -status C:)[11].split(":")[1]
        $row = $dataTable.NewRow()
        $row["System"] = $name
        $row["BitLocker % (C:)"]  = $stat
        $dataTable.Rows.Add($row)
    }  
    $myDataGrid.DataSource = $dataTable
})

# main program body
$myWindow.Text = "BitLocker Status"
$myWindow.size = new-object System.Drawing.Size(500,600)
$myWindow.autoscroll = $true
$myWindow.Add_Shown({$myWindow.Activate()})
$timer.Start()
$myWindow.ShowDialog()

1 个答案:

答案 0 :(得分:0)

我无法相信没有人回答这个问题,也许我不清楚。

无论如何,解决方案很简单,写输出$ object,然后是receive-job。 完成