Not sure if you are aware, but calling a delegate like that via BeginInvoke() 
is really just the same thing as using ThreadPool.QueueUserWorkItem(). You 
might save yourself a lot of hassle by omitting the Class1 and event handler 
funny business, and just pass your callback directly to QUWI:

import clr
clr.AddReference("System")
from System import Threading

def testCallback(foo):
  # print might not be safe here due to threading... but Debug.WriteLine() is 
thread-safe
  from System import Diagnostics
  Diagnostics.Debug.WriteLine(foo)

# test using sync call
testCallback("sync")

# test using async call
cb = Threading.WaitCallback(testCallback)
Threading.ThreadPool.QueueUserWorkItem(cb, "async")


Keith Rome
Senior Consultant and Architect
MCPD-EAD, MCSD, MCDBA, MCTS-WPF, MCTS-TFS, MCTS-WSS
Wintellect | 770.617.4016 | kr...@wintellect.com
www.wintellect.com

-----Original Message-----
From: users-boun...@lists.ironpython.com 
[mailto:users-boun...@lists.ironpython.com] On Behalf Of Matthew Green
Sent: Sunday, April 10, 2011 12:47 AM
To: users@lists.ironpython.com
Subject: [IronPython] BeginInvoke from C# to IronPython

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!!!
_______________________________________________
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