public unsafe delegate int AlarmProc(int saddr, int daddr, int command, byte* data);public delegate void dAttachHandle(int saddr, int daddr, int command, byte[] data);
dAttachHandle te;
//启动监听
public unsafe int StartAttch(int DeviceHandle,dAttachHandle AttachHandleFunction)
{
te = new dAttachHandle(AttachHandleFunction);
AlarmProc ap = new AlarmProc(AttachHandleCS);
return AttachHandle(DeviceHandle, ap); }
private unsafe int AttachHandleCS(int saddr, int daddr, int command, byte* data)
{
byte[] buffer = new byte[7];
for (int i = 0; i < 7; i++)
buffer[i] = data[i];
te(saddr, daddr, command, buffer);
return 0; }错误信息:
A callback was made on a garbage collected delegate of type 'ACS.Reader!ACS.Reader.mmReader+AlarmProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
dAttachHandle te;
//启动监听
public unsafe int StartAttch(int DeviceHandle,dAttachHandle AttachHandleFunction)
{
te = new dAttachHandle(AttachHandleFunction);
AlarmProc ap = new AlarmProc(AttachHandleCS);
return AttachHandle(DeviceHandle, ap); }
private unsafe int AttachHandleCS(int saddr, int daddr, int command, byte* data)
{
byte[] buffer = new byte[7];
for (int i = 0; i < 7; i++)
buffer[i] = data[i];
te(saddr, daddr, command, buffer);
return 0; }错误信息:
A callback was made on a garbage collected delegate of type 'ACS.Reader!ACS.Reader.mmReader+AlarmProc::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.
解决方案 »
- 「控件控」被埋没的控件:FlowLayoutPanel
- 关于数据算法的猜想,请高手进来!
- 求正则表达式
- winform里面我在窗体里面按CTRL+Enter,就可以运行Button事件!
- 【没分了将就点吧】这样为什么取不到节点? treeNode问题
- 请问,大家,如何用c# 打开或关闭一个windows端口吗!....急呀,大侠!.
- 分数全拿出来了!!!
- c#如何记录用户的操作策略
- 菜农求救:哪里有多一点的实例下载阿:~!
- 如何在panel上动态生成一个控件(100分相送,绝不食言,急!!!!!!!)
- 如果哪位大虾有空帮我看看好么?这段代码错在哪里
- string sPath = OpenFileDialog.FileName,如何取得文件名(不包括路径)和文件的路径(不包括文件名)?
不用unsafe也是同样的错误。为什么回调会在垃圾回收里呢
如下试试
public delegate int AlarmProc(int saddr, int daddr, int command, byte[] data);public delegate void dAttachHandle(int saddr, int daddr, int command, byte[] data);
dAttachHandle te;//启动监听
public int StartAttch(int DeviceHandle, dAttachHandle AttachHandleFunction)
{
te = AttachHandleFunction;
AlarmProc ap = new AlarmProc(AttachHandleCS);
return AttachHandle(DeviceHandle, ap);
}private int AttachHandleCS(int saddr, int daddr, int command, byte[] data)
{
byte[] buffer = new byte[7];
for (int i = 0; i < 7; i++)
buffer[i] = data[i];
te(saddr, daddr, command, buffer);
return 0;
}
AlarmProc ap = new AlarmProc(AttachHandleCS);
定义为类成员变量