Hi, using a delegate to handle an event im getting from an api call, only problem is its being destroyed by GC and not sure how to stop this ..
private delegate void StartStopEventDelegate(short Channel, short Event);
[DllImport("c:\\mydll.dll")]
private static extern char SetCallBack_StartStop(StartStopEventDelegate myDelegate);
then use of above code which is done i
SetCallBack_StartStop(startstop);
void startstop(short channel, short event)
{
//lotsa stuff here
}
after i force GC or wait for GC i get
A callback was made on a garbage collected delegate of type
please help
Loading
Mike GoldPosted Aug 1, 2007, 6:15 PM
You might consider doing this PInvoke piece in a separate assembly using managed c++. Managed C++ is a bit more forgiving with unmanaged calls. Here is a link that shows you how to do it.
http://msdn2.microsoft.com/en-us/library/ektebyzx(VS.80).aspx
Daniel van PletzenPosted Aug 1, 2007, 6:08 PM
AlanPosted Aug 1, 2007, 5:48 PM
I'd try 'pinning' the delegate with this line before you make the call:
GCHandle gch = GCHandle.Alloc(startstop, GCHandleType.Pinned);
and when you're finished unpin it and release the handle with:
gch.Free();