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);
It’s not referring to your method
singleFrameEventHandler, it’s referring to the delegate typeFrameEventHandler.Probably something along the lines of
public delegate void FrameEventHandler();in the code for assemblyBeamGage_Interface.update for question update
From this page (can’t find the referenced msdn page)
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 maybeg_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)