[IronPython] BeginInvoke from C# to IronPython

Matthew Green babygguy at gmail.com
Sun Apr 10 06:47:00 CEST 2011


Hi everyone,

I came across a simple C# httplistener
(http://www.paraesthesia.com/archive/2008/07/16/simplest-embedded-web-server-ever-with-httplistener.aspx).
I decided to have a quick play with it and leave the listener in C#
and make the client calls and callback in IronPython. After multiple
attempts I failed to get C#'s BeginInvoke call to callback to
IronPython. That catch shows a "The object must be a runtime
Reflection object." error.

I implemented this short example which failed to callback to the
Python function and hits the catch after trying to BeginInvoke -
----------
C#:
using System;
using System.Globalization;
using System.Net;
using System.Threading;

namespace test
{
    public class Class1
    {
        public event EventHandler<SomeEventArgs> IncomingRequest = null;

        public void testInvoke()
        {
            SomeEventArgs e = new SomeEventArgs(1);
            try
            {
                if (this.IncomingRequest != null)
                {
                    this.IncomingRequest.BeginInvoke(this, e, null, null);
                }
            }
            catch
            {
                //failed
            }
        }
    }
}

public class SomeEventArgs : EventArgs
{
    public int RequestContext;

    public SomeEventArgs(int anarg)
    {
        this.RequestContext = anarg;
    }
}

Python:
import clr
clr.AddReference("test")
from test import *

def testcallback(foo, bar):
    pass

thing = Class1()
thing.IncomingRequest += testcallback
thing.testInvoke()
----------

I get the feeling I'm missing some basic knowledge about how
IronPython functions are added as events. But I feel like I'm getting
distracted and looking at all the wrong things. Thanks for your
help!!!



More information about the Ironpython-users mailing list