# Friday, October 09, 2009

Asynchronous callback delegates are not a friend to PowerShell. They are serviced by the .NET threadpool which means that if they point to script blocks, there will be no Runspace available to execute them. Runspaces are thread-local resources in the PowerShell threadpool. The .NET threadpool, operating independently, is not too interested in coordinating callbacks with PowerShell. So what do we do?

There is one feature of PowerShell 2.0 that is capable of running scriptblocks in a pseudo-asynchronous manner: Eventing. Any events bound to with Register-ObjectEvent, EngineEvent or WmIEvent can have associated scriptblocks that will get executed when the associated event is raised. So, if we can somehow convert an asynchronous callback to a .NET event then we can run scriptblocks in response to Async .NET Callbacks. I’ve written a simple function called New-ScriptBlockCallback that helps us do exactly that:

#requires -version 2.0

function New-ScriptBlockCallback {
    param(
        [parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [scriptblock]$Callback
    )
<#
    .SYNOPSIS
        Allows running ScriptBlocks via .NET async callbacks.

    .DESCRIPTION
        Allows running ScriptBlocks via .NET async callbacks. Internally this is
        managed by converting .NET async callbacks into .NET events. This enables
        PowerShell 2.0 to run ScriptBlocks indirectly through Register-ObjectEvent.         

    .PARAMETER Callback
        Specify a ScriptBlock to be executed in response to the callback.
        Because the ScriptBlock is executed by the eventing subsystem, it only has
        access to global scope. Any additional arguments to this function will be
        passed as event MessageData.
        
    .EXAMPLE
        You wish to run a scriptblock in reponse to a callback. Here is the .NET
        method signature:
        
        void Bar(AsyncCallback handler, int blah)
        
        ps> [foo]::bar((New-ScriptBlockCallback { ... }), 42)                        

    .OUTPUTS
        A System.AsyncCallback delegate.
#>
    # is this type already defined?    
    if (-not ("CallbackEventBridge" -as [type])) {
        Add-Type @"
            using System;
            
            public sealed class CallbackEventBridge
            {
                public event AsyncCallback CallbackComplete = delegate { };

                private CallbackEventBridge() {}

                private void CallbackInternal(IAsyncResult result)
                {
                    CallbackComplete(result);
                }

                public AsyncCallback Callback
                {
                    get { return new AsyncCallback(CallbackInternal); }
                }

                public static CallbackEventBridge Create()
                {
                    return new CallbackEventBridge();
                }
            }
"@
    }
    $bridge = [callbackeventbridge]::create()
    Register-ObjectEvent -input $bridge -EventName callbackcomplete -action $callback -messagedata $args > $null
    $bridge.callback
}
posted on Friday, October 09, 2009 2:09:30 PM (Eastern Daylight Time, UTC-04:00)  #    Comments [1] Trackback
Related posts:
PowerShell 2.0 – Partial Application of Functions and Cmdlets
PowerShell – The Patchwork of Paths, PSPaths and ProviderPaths
PowerShell - Why are keys in Hashtables sorted randomly?
PowerShell 2.0 - About Dynamic Parameters
PowerShell 2.0 – Introducing the PModem File Transfer Protocol
PowerShell 2.0 - Enabling Remoting with Virtual XP Mode on Windows 7

Referred by:
http://twitturls.com/ [Referral]
http://favit.com/ [Referral]
http://www.google.com/reader/view/ [Referral]
http://www.twittergadget.com/gadget.asp?lang=en&country=us&.... [Referral]
app:/index.html [Referral]
http://powertwitter.me/ [Referral]
http://twitter.com/ [Referral]
powershell 2.0 async (www.google.com) [Referral]
What are asynchronous callbacks in .net (www.google.com) [Referral]
http://www.google.com/ig?hl=en&source=iglk [Referral]
http://www.google.co.uk/reader/view/?hl=en&tab=ly [Referral]
http://www.facebook.com/home.php [Referral]
http://bit.ly/IAh02 [Referral]
http://bit.ly/3jxVR7 [Referral]
#powershell (twitter.com) [Referral]
http://www.google.com/reader/view/?utm_campaign=en&utm_sourc... [Referral]
run functions from powershell 2.0 (www.google.com.br) [Referral]
http://pipes.yahoo.com/pipes/pipe.info?_id=uAmYy9xq3BGHcV361... [Referral]
http://storage.conduit.com/4/143/CT1437604/Gadgets/4066044a-... [Referral]
compact framework 2.0 callbacks (www.google.co.uk) [Referral]
http://www.netvibes.com/ [Referral]
powershell 2.0 .net 4.0 (www.google.com) [Referral]
powershell function asynchronus (www.google.ca) [Referral]
http://www.feedly.com/home [Referral]
powershell 2.0 (www.google.nl) [Referral]
callbacks in .net (www.google.co.in) [Referral]
.net callback (www.google.com) [Referral]
asp.net asynchronous callback (www.google.com) [Referral]
powershell 2 async (www.google.com) [Referral]
http://delicious.com/save?url=http%3A%2F%2Fwww.nivot.org%2FC... [Referral]
"Register-ObjectEvent" asynchronous callback (www.google.com) [Referral]
powershell 2 eventing (www.google.com) [Referral]
how to call a function async in powershell (www.google.com) [Referral]
what is callback in .net (www.google.com) [Referral]
running .net in powershell (www.google.pl) [Referral]
"powershell 2.0" delegates (www.google.com) [Referral]
powershell add-type public Point[] (www.google.fr) [Referral]
powershell 2.0 functions (www.google.com) [Referral]
powershell 2.0 async (www.google.com) [Referral]
http://storage.conduit.com/ (www.google.es) [Referral]
asynchronous callback moss 2007 (www.google.com) [Referral]
asynchronous callback with asp.net (www.google.com.eg) [Referral]
powershell functions asynchronous (www.google.de) [Referral]
powershell async OR asynchronous script block (www.google.com) [Referral]
script block does not run after callback (www.google.co.uk) [Referral]
asp net asynchronous callback (www.google.ca) [Referral]
powershell 2.0 using delegates (www.google.pl) [Referral]
powershell callbacks events (www.bing.com) [Referral]
callback powershell (www.google.co.jp) [Referral]
Asynchronous callbacks (www.google.cn) [Referral]
Powershell 2 event asynchronous (www.google.de) [Referral]
powershell 2.0 async (www.google.com) [Referral]
call a powershell function asynchronously (www.google.com) [Referral]
callbackcomplete event (www.google.it) [Referral]
"powershell 2" get-delegate (www.google.com) [Referral]
"powershell 2" delegate (www.google.com) [Referral]
asynchronous delegates in asp.net +facebook (www.google.co.in) [Referral]
can Register-ObjectEvent action be a function (www.google.com) [Referral]
реферралы + "asp.net" + робот (www.google.ru) [Referral]
.net 4.0 callbacks (www.google.com) [Referral]
powershell 2.0 function (www.google.com) [Referral]
powershell thread pool (www.google.de) [Referral]
callback callbackcomplete (www.google.it) [Referral]
how to handle asynchronous responses .NET (www.google.co.in) [Referral]
asp.net run powershell async (www.google.is) [Referral]
powershell callback (www.google.com) [Referral]
using callback in powershell (www.google.de) [Referral]
powershell async call (www.google.it) [Referral]
running .net from powershell (www.google.com) [Referral]
.net threadpool callback (www.google.com) [Referral]
powershell async (www.google.com.ua) [Referral]
powershell async function (www.bing.com) [Referral]
powershell ".net 4" (www.bing.com) [Referral]
asincronos callback (www.google.com.mx) [Referral]
powershell .net events (www.google.com) [Referral]
powershell callback (www.google.com) [Referral]
PowerShell callback (www.google.co.jp) [Referral]
powershell 2.0 Asynchronous function (www.google.ru) [Referral]
.net asynchronous callback handler (www.google.com) [Referral]
asynchronous delegate+facebook app (www.google.co.in) [Referral]
asp.net async callback (www.google.com) [Referral]
- What are asynchronous callbacks (www.google.com) [Referral]
"register-objectevent" +action arguments (www.google.com) [Referral]
callbacks powershell (www.google.com.au) [Referral]
callbacks in powershell (www.bing.com) [Referral]
http://powershell-scripting.com/index.php?option=com_joomlab... [Referral]
http://powershell-scripting.com/index.php?option=com_joomlab... [Referral]
powershell async (www.google.co.uk) [Referral]
powershell callback (www.google.com) [Referral]
asynchronous callback delegate (www.google.co.in) [Referral]
powershell script block delegate (www.google.com) [Referral]
asynchronous function call powershell (www.google.com) [Referral]
powershell thread pool (www.google.com.au) [Referral]
asp.net powershell 2.0 (www.google.cz) [Referral]
SOA and asynchronous Callbacks (www.google.com) [Referral]
http://www.step-two.ru/ [Referral]
powershell remoting asynchronous -asjob (www.google.com) [Referral]
PowerShell ScriptBlock callback (www.bing.com) [Referral]
pseudo async call (www.google.co.uk) [Referral]
powershell 2 events (www.google.com) [Referral]
callbackcomplete (www.google.com) [Referral]
powershell from asynchronous (www.google.co.uk) [Referral]
powershell 2.0 delegate (www.google.com) [Referral]
powershell 2 delegate (www.google.co.uk) [Referral]
delegate powershell 2 (www.google.com) [Referral]
powershell Async (www.google.com) [Referral]
powershell call function async (www.google.com.au) [Referral]
powershell async (www.google.com) [Referral]
run powershell 2.0 in ASP.net (www.google.se) [Referral]
powershell 2 delegate (www.google.com) [Referral]
register .net event callback in powershell (www.google.com) [Referral]
Powershell 2 Eventing -CTP2 (www.google.ch) [Referral]
async with callback handler compact framework (www.google.com) [Referral]
objectevent powershell how to (www.google.com) [Referral]
.net argument if async callback (www.google.pl) [Referral]
PowerShell New-Delegate (www.google.com) [Referral]
powershell async delegate (www.bing.com) [Referral]
powershell delegate scriptblock (www.bing.com) [Referral]
powershell v2.0 delegates (www.google.com) [Referral]
powershell threadpool (www.google.ca) [Referral]
asynchronous function in .NET (www.google.com) [Referral]
NET Events in PowerShell (www.google.ru) [Referral]
asp.net powershell 2.0 (www.google.com) [Referral]
powershell and ".net 4" (www.google.com) [Referral]
configure PowerShell to run with ".net 4.0" (www.google.com) [Referral]
get callback response SharePoint (www.google.pl) [Referral]
powershell 2.0 create delegate (www.google.co.in) [Referral]
powershell time callback (www.google.com) [Referral]
powershell v2.0 delegates (www.google.com) [Referral]
http://yandex.ru/yandsearch?text=.net+async+callback&lr=213&... [Referral]
powershell callback (www.google.com) [Referral]
powershell 2.0 asynchronous (www.google.com.au) [Referral]
powershell async (www.google.cz) [Referral]
http://polprav.blogspot.com/ [Referral]
powershell asynch (www.google.cn) [Referral]
asp.net 2008 asynchronous callback (www.google.com) [Referral]
powershell threadpool (www.google.com) [Referral]
powershell handle .net events (www.google.com) [Referral]
powershell Threadpool (www.google.co.uk) [Referral]
powershell .NET 4.0 (www.bing.com) [Referral]
powershell delegates (www.google.com.jm) [Referral]
powershell dot net attributes (www.google.com) [Referral]
linkedin yahoo WMI Callback (www.google.co.in) [Referral]
wcf async powershell (www.google.com) [Referral]
powershell calling delegate (www.google.at) [Referral]
powershell mta objectevent oisin (www.bing.com) [Referral]
powershell asynchron function call (www.google.de) [Referral]
powershell and wcf callbacks (www.bing.com) [Referral]
2 net compute (search.yahoo.com) [Referral]
linkedin yahoo WMI Callback (www.google.co.za) [Referral]
add delegates +powershell (www.google.co.uk) [Referral]
call powershell 2.0 in .Net (www.google.com) [Referral]
powershell function parameter callback (www.google.com) [Referral]
run dot net in powershell (www.google.com) [Referral]
powerShell 2.0 delegates (www.google.de) [Referral]
powershell delegate (www.bing.com) [Referral]
register-objectevent powershell timer (www.google.com) [Referral]
powershell threading 2.0 (www.google.com) [Referral]
"powershell 2" delegate (www.google.de) [Referral]
.net remoting callback asp.net (www.google.co.kr) [Referral]
powershell 2 .net event (www.google.cn) [Referral]
callback handler powershell (www.google.com) [Referral]
powershell delegate functions (www.google.com) [Referral]
biztalk wcf asynchronous callback (www.google.nl) [Referral]
.net powershell 2 (www.google.com) [Referral]
call powershell from asp (www.google.com) [Referral]
powershell 2.0 get response (www.google.ca) [Referral]
asp.net async callback WCF (www.google.com) [Referral]
powershell asynchronous stream read (www.google.de) [Referral]
.net callback (www.bing.com) [Referral]
powershell 2.0 delegates (www.google.com) [Referral]
http://nova.rambler.ru/search?btnG=%D0%9D%D0%B0%D0%B9%D1%82%... [Referral]
powershell convert scriptblock to delegate (www.google.com) [Referral]
asynchronous callback using delegates in asp.net (www.google.com) [Referral]
What are asynchronous callbacks + asp.net 2.0 (www.google.co.in) [Referral]
asynchronous callback in asp.net (www.google.co.in) [Referral]
New-Delegate powershell (www.google.com) [Referral]
wcf asynchronous callback (www.google.com) [Referral]
asynchronous callback in asp.net (www.google.co.in) [Referral]
powershell event asynchronous (www.google.co.uk) [Referral]
threading powershell 2.0 (www.google.com) [Referral]
powershell 2.0 callback (www.google.com) [Referral]
powershell function as delegate (www.google.com) [Referral]
powershell async callback (www.google.com) [Referral]
AsyncCallback wcf (www.google.com) [Referral]
http://www.rambler.ru/srch?words= [Referral]
powershell register-objectevent scriptblock (www.bing.com) [Referral]
powershell and .net 4.0 (www.bing.com) [Referral]
async function call powershell (www.google.com) [Referral]
asp.net asynchronous thread callback (www.google.co.uk) [Referral]
powershell asynchron (www.google.at) [Referral]
Powershell ThreadPool (www.google.com.au) [Referral]
asynchronous powershell (www.google.com) [Referral]
powershell callbacks (www.google.com) [Referral]
powershell callback (www.google.com) [Referral]
powershell callback example (www.google.com) [Referral]
powershell asynchron (www.google.ch) [Referral]
powershell thread (www.google.com) [Referral]
asp.net asynchronous callback (www.google.com) [Referral]
asp.net powershell async (www.google.co.il) [Referral]
register-objectevent powershell (www.bing.com) [Referral]
VSTO asynchronous event call (www.google.com) [Referral]
powershell event asyncronous (www.google.com) [Referral]
powershell V2 create delegate (www.google.com) [Referral]
system.asynccallback powershell (www.google.co.kr) [Referral]
powershell "no Runspace available" (www.google.com) [Referral]
wcf async callback (www.google.ru) [Referral]
powershell 2.0 eventing (www.google.com.au) [Referral]
powershell thread 2.0 (www.google.com) [Referral]
asynccallback wcf (www.google.co.uk) [Referral]
powershell threading 2.0 (www.google.com) [Referral]
add async callback methods in a class +dot net 2.0 (www.google.co.in) [Referral]
WCF asynchronous callback (www.google.com) [Referral]
powershell callback (www.google.com) [Referral]
powershell 2.0 system.delegate (www.google.com) [Referral]
wcf callback asp.net page (www.google.nl) [Referral]
register-objectevent function (www.bing.com) [Referral]
access wcf in asynchronous manner (www.google.com) [Referral]
powershell asynchronous (www.google.com) [Referral]
powershell with .NET 4.0 (www.google.com) [Referral]
powerShell 2.0 async Event (www.google.de) [Referral]
powershell v2 asynchronous (www.google.be) [Referral]
.net remoting callback asynchronous (www.google.com) [Referral]
powershell v2.0 thread (www.google.com.au) [Referral]
.net callback response type (www.google.co.in) [Referral]
asynchronous callback delegate .net (www.google.com) [Referral]
.net compact wcf async (www.google.com) [Referral]
Powershell thread pool (www.google.com) [Referral]
powershell 2 delegates (www.google.de) [Referral]
powershell .NET thread pool (www.google.com) [Referral]
powershell asynchronous callback (www.google.com) [Referral]
powershell add delegate (www.google.com) [Referral]
.net events powershell 2.0 (www.bing.com) [Referral]
threadpool powershell (www.google.com) [Referral]
powershell scriptblock new thread (www.google.cz) [Referral]
how to do asynchronous callback handlers in .net compact framework (www.bing.com) [Referral]
powershell delegate (www.bing.com) [Referral]
wcf asynchronous callback (www.google.com) [Referral]
linkedin yahooWMICallback (www.google.co.in) [Referral]
PowerShell delegate (www.google.co.jp) [Referral]
PowerShell delegate (www.google.co.jp) [Referral]
powershell threadpool (www.google.hu) [Referral]
http://www.powershell-scripting.com/index.php?option=com_joo... [Referral]
threadpool callback wcf (www.google.com.by) [Referral]
powershell convert to scriptblock (www.bing.com) [Referral]
powershell 1 start scripts async (www.google.be) [Referral]
powershell 2.0 threading (www.google.de) [Referral]
asynchronous callback powershell (www.google.com) [Referral]
powershell asynchronous callbacks (www.bing.com) [Referral]
create delegates powershell 2.0 (www.google.com) [Referral]
powershell 2.0 async (www.google.dk) [Referral]
threadpool .net callback (www.google.com) [Referral]
wcf callback stream (www.google.com.tr) [Referral]
powershell asynchronously (www.google.bg) [Referral]
"Register-ObjectEvent" powershell (www.google.com) [Referral]
powershell .net event (www.google.com) [Referral]
add delegate powershell (www.google.fr) [Referral]
MOSS 2007 Asynchronous function (www.google.com) [Referral]
powershell ThreadPool (www.bing.com) [Referral]
.net compact async thread (www.google.com) [Referral]
using powershell script blocks as .net delegates (www.google.com) [Referral]
.net compact framework asynchronous delegates (www.bing.com) [Referral]
powershell 2 delegate (www.google.com) [Referral]
wcf callback example for asynchronous (www.google.co.in) [Referral]
New-ScriptBlockCallback (www.bing.com) [Referral]
class dot net powershell timer (www.google.ca) [Referral]
wcf asynchronous callback (www.google.com) [Referral]
Powershell 2.0 timer event (www.google.com) [Referral]
powershell Register-Event net (www.google.co.uk) [Referral]
powershell call script asynchronous (www.google.com) [Referral]
powershell event function callback (www.google.co.uk) [Referral]
powershell event function callback (www.google.co.uk) [Referral]
.net async event (www.bing.com) [Referral]
wcf asynchronous callback sample (www.google.com) [Referral]
powershell 2.0 .net (www.google.com) [Referral]
WCF AsyncCallback .net (www.google.com) [Referral]
wcf asynchronous callback (www.google.com) [Referral]
"Powershell 2.0" threads (www.google.se) [Referral]
powershell php function call (www.google.de) [Referral]
wcf asynchronous get parameter of function (www.google.com.vn) [Referral]
powershell 2.0 asynchronus (www.google.at) [Referral]
async callback sharepoint (www.google.com) [Referral]
asynchronous callback remoting (www.google.com) [Referral]
asynchronous callback in asp.net tutorial (www.google.com) [Referral]
powershell v2 forms event asynchronous (www.google.fr) [Referral]
powershell static callbacks (www.google.com) [Referral]
threading powershell (www.google.com) [Referral]
powershell .net (www.google.com) [Referral]
asynchronous callback in powershell (www.google.com) [Referral]
callback in .net compact framework (www.google.co.uk) [Referral]
powershell start assync script (www.google.hu) [Referral]
powershell timer (www.google.com) [Referral]
register-objectevent messagedata (www.google.com) [Referral]
asynchronous WCF 4.0 (www.bing.com) [Referral]
powershell delegate handler (www.google.com) [Referral]
powershell delegate handler windows 7 (www.google.com) [Referral]
powershell 2.0 delegate return (www.google.com) [Referral]
async call in moss 2007 (www.google.ru) [Referral]
Monday, October 12, 2009 1:28:58 AM (Eastern Daylight Time, UTC-04:00)
Do you have any tip for me where to start if I would like to learn about runspaces, threadpools etc.?
Then the post would make more sense for me ;) Thank you.
Comments are closed.