This is actually a CLR bug.  A simple repro of the issue is below if anyone is 
curious.

The CLR team has resolved this bug as "Won't Fix" and says a workaround is 
available.  The
only workaround I can think of is explicitly queueing the work item to the 
thread pool and
provide an object for you to get state back on.  I've asked them for more 
information.

Unfortunately it means that currently this just won't work in IronPython on MS 
.NET at 
all today :(.

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Globalization;

class Test {
    delegate void MyDelegate();

    public static void Main(string[]args) {
        DynamicMethod test = new DynamicMethod("Hello", 
            typeof(void), 
            new Type[]{typeof(string)}, 
            typeof(string).Module);

 

        MethodInfo writeString = typeof(Console).GetMethod("WriteLine", 
            new Type[]{typeof(string)}); 

        ILGenerator il = test.GetILGenerator(256);
        il.Emit(OpCodes.Ldarg_0);
        il.EmitCall(OpCodes.Call, writeString, null);
        il.Emit(OpCodes.Ret);


        MyDelegate dlg =  (MyDelegate) test.CreateDelegate(typeof(MyDelegate), 
"Hello World!");
        dlg.BeginInvoke(new AsyncCallback(Finished), null);
        Console.ReadLine();
    }


    public static void Finished(IAsyncResult ar) {
    }
}



> -----Original Message-----
> From: users-boun...@lists.ironpython.com [mailto:users-
> boun...@lists.ironpython.com] On Behalf Of Schmottlach, Glenn
> Sent: Monday, May 04, 2009 2:58 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] Making asynchronous calls in IronPython
> 
> Okay, call me clueless . . .
> 
> This is what I have so far as an example:
> 
> import clr
> clr.AddReference('IronPython')
> 
> from System import *
> from System.Threading import Thread
> from IronPython.Compiler import CallTarget0
> 
> class Foo:
>     def start(self):
>         print 'Started.'
>         Thread.Sleep(3000)
>         threadId = System.Threading.Thread.CurrentThread
>         return 'Exiting thread %r' % (threadId)
> 
>     def finish(self, r):
>         print 'Finished.'
> 
> 
> if __name__ == '__main__':
>       f = Foo()
>       caller = CallTarget0(f.start)
>       result = caller.BeginInvoke(None, None)
>       caller.EndInvoke(result)
> 
> 
> Can you give me a concrete example of using CallTarget0 and
> AsyncCallback to implement this? What it does now is print:
> 
> Finished.
> SystemError:Object reference not set to an instance of an object.
> 
> Obviously, I'm not using AsyncCallback because it's not clear to me
> what
> I pass to the constructor and where I pass the subsequent object.
> 
> If you could spare me a few moments and correct this simple sample it
> would be very grateful.
> 
> Thanks . . .
> 
> 
> -----Original Message-----
> From: users-boun...@lists.ironpython.com
> [mailto:users-boun...@lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Monday, May 04, 2009 5:48 PM
> To: Discussion of IronPython
> Subject: Re: [IronPython] Making asynchronous calls in IronPython
> 
> Schmottlach, Glenn wrote:
> > I've looked at the C# examples in MSDN but they don't appear to lend
> > themselves (at least with my limited experience in .NET) to an easy
> > implementation in IronPython due to the dynamic nature of the type
> > system in Python.
> >
> > The example I looked at:
> >
> > http://msdn.microsoft.com/en-us/library/h80ttd5f.aspx
> >
> >
> > declares a delegate type which seems difficult to do in IronPython,
> e.g.
> >
> > C#
> >
> > public delegate bool AsyncFactorCaller (
> >              int number,
> >              ref int primefactor1,
> >              ref int primefactor2);
> >
> > So what's the equivalent in IronPython?
> >
> > Also, (out) parameters are passed by reference to BeginInvoke and
> > EndInvoke and it's not clear to me how to handle this in IronPython.
> >
> > So, I suspect I'm missing something in order to get things working.
> What
> > I really need to see is a concrete IronPython example and I think
> things
> > would gel at that point. Could you offer such an example . . . Google
> > hasn't been helpful in this respect.
> >
> 
> out parameters are handled by IronPython as extra return values - you
> wrap your callback function in CallTarget0 and an AsyncCallback.
> 
> I hope this helps.
> 
> Michael
> 
> >
> > -----Original Message-----
> > From: users-boun...@lists.ironpython.com
> > [mailto:users-boun...@lists.ironpython.com] On Behalf Of Michael
> Foord
> > Sent: Monday, May 04, 2009 4:33 PM
> > To: Discussion of IronPython
> > Subject: Re: [IronPython] Making asynchronous calls in IronPython
> >
> > Schmottlach, Glenn wrote:
> >
> >> Although I'm very comfortable with CPython, I'm new to IronPython
> and
> 
> >> the .NET framework. What I am looking for is sample code that shows
> >> how to issue an asynchronous call from IronPython using the
> >> BeginInvoke/EndInvoke mechanism of .NET.
> >>
> >>
> >
> > I can't remember the usage off the top of my head - but usage from
> > IronPython is identical to usage from C#, so an MSDN example ought to
> > give you what you need.
> >
> > All the best,
> >
> > Michael
> >
> >> Currently, I'm playing around with GTK# and a D-Bus .NET
> >> implementation (NDesk-DBus) that only provides synchronous proxy
> >> methods for D-Bus services. I believe (please correct me if I'm
> wrong)
> >>
> >
> >
> >> that I should be able to use the BeginInvoke/EndInvoke mechanism to
> >> issue these blocking proxy requests asynchronously and be notified
> >> (via a callback) when the method has completed.
> >>
> >> My blocking proxy calls look something like this:
> >>
> >> ret = /self/._proxy.TunerGetCurrentWaveBand(0)
> >>
> >> where 'ret' returns the current waveband (AM, FM, etc..) and are
> >> called from a button "clicked" delegate. I would rather not block
> the
> 
> >> GTK# dispatch loop while waiting for the reply from the service. Can
> >> someone offer me any advice or provide a reference to sample code
> that
> >>
> >
> >
> >> illustrates the basic concept from IronPython?
> >>
> >>
> >>
> >
> -----------------------------------------------------------------------
> -
> >
> >> _______________________________________________
> >> Users mailing list
> >> Users@lists.ironpython.com
> >> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> >>
> >>
> >
> >
> >
> 
> 
> --
> http://www.ironpythoninaction.com/
> http://www.voidspace.org.uk/blog
> 
> 
> _______________________________________________
> Users mailing list
> Users@lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
> 
> _______________________________________________
> Users mailing list
> Users@lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to