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);