C#中用委托实现C++的回调函数
生活随笔
收集整理的這篇文章主要介紹了
C#中用委托实现C++的回调函数
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C++中抓圖回調(diào)函數(shù)
void (CALLBACK* DisplayCBFun)(long nPort,char * pBuf,long nSize,long nWidth,long nHeight,long nStamp,long nType,long nReceaved));C#中定義為委托
public delegate void DisplayCBFun(int nPort, IntPtr pBuf, int nSize, int nWidth, int nHeight, int nStamp, int nType, int nReceaved);C#中回調(diào)函數(shù)的實現(xiàn)
public void CB_DisplayCBFun(int nPort, IntPtr pBuf, int nSize, int nWidth, int nHeight, int nStamp, int nType, int nReceaved){ }由于.Net的垃圾回收機制,因此在這類應(yīng)用中,對委托進行垃圾回收后,委托再進行回調(diào),將回引發(fā)CallbackOnCollectedDelegate異常。因此需要將委托聲明為成員變量。
private DisplayCBFun dcbf; private void Function() {dcbf = new DisplayCBFun(CB_DisplayCBFun);HikPlayer.PlayM4_SetDisplayCallBack(nPort, dcbf); }然而我發(fā)現(xiàn)仍然會引發(fā)異常,很可能是頻繁回調(diào)引發(fā)的。權(quán)威資料顯示使用 GC.KeepAlive 來確保特定實例保持活動狀態(tài)一段時間,能解決此問題。
private DisplayCBFun dcbf; private void Function() {dcbf = new DisplayCBFun(CB_DisplayCBFun);HikPlayer.PlayM4_SetDisplayCallBack(nPort, dcbf);//解決方法 GC.KeepAlive(dcbf); }轉(zhuǎn)載自:http://www.cnblogs.com/cyrix/articles/1771491.html?內(nèi)容.
轉(zhuǎn)載于:https://www.cnblogs.com/tianciliangen/p/3885306.html
總結(jié)
以上是生活随笔為你收集整理的C#中用委托实现C++的回调函数的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jpa的好伙伴QueryDSL快速入门
- 下一篇: 软件自动安装管理器 -提供源码下载