views:

32

answers:

1

I have a function that I want to use as an event handler:

void singleFrameEventHandler(void) {
    SetEvent(g_hSingleFrameArrived);
}

However, when I try to register for the event:

iaframe->OnNewFrame += gcnew newFrame(&singleFrameEventHandler);

The following exception is raised:

An unhandled exception of type 'System.NotSupportedException' occurred in mscorlib.dll

Additional information: Serialization of global methods (including implicit serialization via the use of asynchronous delegates) is not supported.

Is there some way to get around this?


Edit:

I have changed the code so now the event handler is a method in a class. However, now I get a different exception:

An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll

Additional information: Unable to find assembly 'BeamGage_Interface, Version=1.0.3882.24450, Culture=neutral, PublicKeyToken=null'.

If it's relevant, the class definition looks like this:

[System::Serializable]
ref class FrameEventClass {
public:
    FrameEventClass(const char *newId, IAFrame ^ frame) : id(newId) {
        frame->OnNewFrame += gcnew newFrame(this, &FrameEventClass::frameEventHandler);
    }
private:
    const char *id;
    void frameEventHandler(void) {
        //Signal that the frame has arrived
        SetEvent(g_hSingleFrameArrived);
        //unregister for event
        IAFrame ^ frame = /* Code to get handle to frame object */;
        frame->OnNewFrame -= gcnew newFrame(this, &FrameEventClass::frameEventHandler);
        return;
    }
};

And here is how I register for the event:

gcnew FrameEventClass(id, iaframe);
+1  A: 

Type 'FrameEventHandler' ... is not marked as serializable

It's not referring to your method singleFrameEventHandler, it's referring to the delegate type FrameEventHandler.

Probably something along the lines of public delegate void FrameEventHandler(); in the code for assembly BeamGage_Interface.

update for question update
From this page (can't find the referenced msdn page)

The common language runtime does not support serialization of global methods, so delegates cannot be used to execute global methods in other application domains.

Other CLR languages (c# etc) don't support global methods so you can't use them with the CLR.

Your solution (I believe) is to wrap singleFrameEventHandler (or maybe g_hSingleFrameArrived, I can't tell from your snippets) in a class and pass the instance method instead. Something like (forgive my lack of recent c++ usage, I may make mistakes)

class MyFrameEventHandler
{
    public:
        void singleFrameEventHandler(void){
            SetEvent(g_hSingleFrameArrived);
        }
}
// Usage
MyFrameEventHandler handler;
iaframe->OnNewFrame += gcnew newFrame(&handler.singleFrameEventHandler);
BioBuckyBall
Alright, it turns out I was debugging a different version of the code than what I was editing and building. I edited the post to reflect the exception that is being raised due to my current code.
Eric Finn