延迟执行后台工作人员的DoWork事件

时间:2014-01-10 14:41:13

标签: powershell backgroundworker powershell-v4.0

我正在解决PowerShell 4.0脚本中的问题。我使用windows Forms命名空间构建了一个用户界面,但需要花费一些时间进行处理,这就是我采用BackgroundWorker来保持GUI响应的原因。

一旦我开始使用$Worker.Add_DoWork({})方法添加事件订阅,但这不起作用。这就是我使用Register-ObjectEvent代替的原因。现在我的DoWork事件只在我关闭我的表格后执行,我不明白为什么会发生这种情况。我的代码如下,任何人都可以帮我解决这个问题吗?

#Create worker object
$Worker = new-object System.ComponentModel.BackgroundWorker;
$Worker.WorkerReportsProgress = $true;

#Handles $Worker.ReportProgress event
$ReportProgress = {$Progressbar.PerformStep()}; 

#Handles $Worker.DoWork event
$DoWork = { write-host("do work event fired")};

#Add eventhandlers
Register-ObjectEvent -InputObject $Worker -EventName DoWork -Action $DoWork ;
Register-ObjectEvent -InputObject $Worker -EventName ProgressChanged -Action $ReportProgress; 

如果我在脚本中调用$Worker.RunWorkerAsync()方法,则不会生成任何输出。如果我关闭调用$Worker.RunWorkerAsync()方法的表单,则会将文本“Do work event fired”输出到PowerShell控制台。

1 个答案:

答案 0 :(得分:2)

要回答我自己的问题,这里是更新进度条的文件复制示例。 Copy-File方法取自我在线程Progress during large file copy (Copy-Item & Write-Progress?)中由stej给出的示例,我根据自己的需要进行了修改。只需在主脚本中侦听后台作业中的事件。

#Register event
Register-EngineEvent -SourceIdentifier Progress -Action {
                        $Progressbar.Value = $event.MessageData; 
                        $StatusText.Text = "Copying VHD File - " + $event.MessageData + " % complete"; 
                        $Form.Refresh();} >null

#Create worker and perform the work
$worker = start-job -name "Work" -scriptblock {    
Register-EngineEvent -SourceIdentifier Progress -Forward;

function Copy-File 
{
    param( [string]$from, [string]$to)
    $ffile = [io.file]::OpenRead($from)
    $tofile = [io.file]::OpenWrite($to)

    try {

        [byte[]]$buff = new-object byte[] 4096 #(4096*1024)
        [long]$total = [long]$count = 0
        do {
            $count = $ffile.Read($buff, 0, $buff.Length)
            $tofile.Write($buff, 0, $count)
            $total += $count
            [int]$pctcomp = ([int]($total/$ffile.Length* 100));
            if ($total % 1mb -eq 0) {
                   New-Event -SourceIdentifier Progress -MessageData  $pctcomp;
            }
        } while ($count -gt 0)

    }

    finally {
         $ffile.Close();
         $tofile.Close();
        }
}

Copy-File -from ("F:\HIS0402_C.wim") -to ("X:\JT\HIS0164.vhd");
};