[Mono-devel-list] Calling C# delegate from native code on a different thread.

Daniel L. Christensen dachristensen at gw.novell.com
Wed Jul 21 15:57:00 EDT 2004

I'm not really embedding mono in my C/C++ application. Rather, I'm using
mono and p/invoke to access some native code that I already have. I
could add a call to mono_thread_attach() but that would mean that my
native api that previously had no dependancy on mono would need to be
linked to it. When the delegate gets marshalled to a fucntion pointer a
bunch of native code gets generated. It seems like the call to
mono_thread_attach() should be embedded somewhere in that code. 
I haven't had a chance yet to build my app under windows yet (that
comes later)... does anyone know whether or not I will have similar
problems in Windows? Do you have to do something special before calling
a marshalled delegate on a different thread or does it just work?

>>> Zoltan Varga <vargaz at gmail.com> 7/20/2004 12:11:01 PM >>>


  The function is called mono_thread_attach (). For more info on the
mono embedding



----- Original Message -----
From: Daniel L. Christensen <dachristensen at gw.novell.com>
Date: Wed, 14 Jul 2004 12:03:16 -0600
Subject: [Mono-devel-list] Calling C# delegate from native code on a
different thread.
To: mono-devel-list at lists.ximian.com


      I'm trying to wrap a native C++ API with C#. I've run into a
spot where the C# object needs to register for an event that occurs on
the native C++ object. So I pass down a delegate which gets converted
to a function pointer on the native side, wrap the funciton pointers
with an event handler object and register the event handler object
with the native C++ object. Certain actions performed by the C++
object happen asynchronously (i.e. on another thread that mono doesn't
know about). When the action completes (or is in progress) it
generates a native event. The native object calls back on the event
handler which in turn tries to call back up into the C# code using the
function pointer that was created from the delegate. When this happens
I get the following error:


      ** ERROR **: file mini.c: line 6508 (mono_get_lmf_addr): should
not be reached



      I imagine that the problem is that I am trying to communicate to
the mono vm from a thread that is not attached to it. When attempting
a similar operation in Java/JNI, you are required to attach the JVM to
the native thread before trying to call up into java. Does mono have a
similar convention? How is this accomplished? Or, is there a better
way to accomplish this using mono?



      Dan Christensen
Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20040721/47ba167b/attachment.html 

More information about the Mono-devel-list mailing list