我需要下载使用WebClient
文件中的PowerShell 2.0,我想展示下载进度,所以我也这样说:PowerShell的:运行空间问题DownloadFileAsync
$activity = "Downloading"
$client = New-Object System.Net.WebClient
$urlAsUri = New-Object System.Uri($url)
$event = New-Object System.Threading.ManualResetEvent($false)
$downloadProgress = [System.Net.DownloadProgressChangedEventHandler] {
$progress = [int]((100.0 * $_.BytesReceived)/$_.TotalBytesToReceive)
Write-Progress -Activity $activity -Status "${progress}% done" -PercentComplete $progress
}
$downloadComplete = [System.ComponentModel.AsyncCompletedEventHandler] {
Write-Progress -Activity $activity -Completed
$event.Set()
}
$client.add_DownloadFileCompleted($downloadComplete)
$client.add_DownloadProgressChanged($downloadProgress)
Write-Progress -Activity $activity -Status "0% done" -PercentComplete 0
$client.DownloadFileAsync($urlAsUri, $file)
$event.WaitOne()
我得到一个错误There is no Runspace available to run scripts in this thread.
为代码在$downloadProgress
处理程序中,这是合乎逻辑的。但是,如何为(可能)属于ThreadPool
的线程提供Runspace
?
UPDATE: 注意既这个问题的答案是值得一读的,如果我能我会接受这两种。
谢谢,这是纯粹的天才。比我的解决方案好得多。现在我会稍微等一下,看看有没有人在接受之前回答Runspace部分(只是好奇它是如何处理的)。 – 2011-02-07 21:08:52
我会为另一种选择提供灵感:) – stej 2011-02-07 21:30:25