Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Thanks stej for the nod. </p> <p>Andrey, powershell has its own threadpool and each service thread keeps a threadstatic pointer to a runspace (the System.Management.Automation.Runspaces.Runspace.DefaultRunspace static member exposes this - and would be a null ref in your callbacks.) Ultimately this means it's difficult - especially in script - to use your own threadpool (as is provided by .NET for async methods) to execute scriptblocks.</p> <p><strong>PowerShell 2.0</strong></p> <p>Regardless, there is no need to play with this as powershell v2 has full support for eventing: </p> <pre><code>$client = New-Object System.Net.WebClient $url = [uri]"http://download.microsoft.com/download/6/2/F/" + "62F70029-A592-4158-BB51-E102812CBD4F/IE9-Windows7-x64-enu.exe" try { Register-ObjectEvent $client DownloadProgressChanged -action { Write-Progress -Activity "Downloading" -Status ` ("{0} of {1}" -f $eventargs.BytesReceived, $eventargs.TotalBytesToReceive) ` -PercentComplete $eventargs.ProgressPercentage } Register-ObjectEvent $client DownloadFileCompleted -SourceIdentifier Finished $file = "c:\temp\ie9-beta.exe" $client.DownloadFileAsync($url, $file) # optionally wait, but you can break out and it will still write progress Wait-Event -SourceIdentifier Finished } finally { $client.dispose() } </code></pre> <p><strong>PowerShell v1.0</strong></p> <p>If you're stuck on v1 (this is not specifically for you as you mention v2 in the question) you can use my powershell 1.0 eventing snap-in at <a href="http://pseventing.codeplex.com/" rel="noreferrer">http://pseventing.codeplex.com/</a></p> <p><strong>Async Callbacks</strong></p> <p>Another tricky area in .NET is async callbacks. There is nothing directly in v1 or v2 of powershell that can help you here, but you can convert an async callback to an event with some simple plumbing and then deal with that event using regular eventing. I posted a script for this (New-ScriptBlockCallback) at <a href="http://poshcode.org/1382" rel="noreferrer">http://poshcode.org/1382</a></p> <p>Hope this helps,</p> <p>-Oisin</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload