回调垃圾收集委托错误 [英] callback on a garbage collected delegate error

查看:63
本文介绍了回调垃圾收集委托错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有一个使用DLL和回调的应用程序。它生成随机的

错误对垃圾收集的代理进行回调。


我发现一些文章指向代表的指针必须有一个

的生命周期参考,这样它就不会被垃圾收集。但我找不到

任何一个例子如何做到这一点。


有人可以帮忙吗?


- -

rgds,Wilfried
http://www.mestdagh。商业

推荐答案

Wilfried,


只需将代理参考分配给一个字段具有与回调相同的生命周期的类。因此,如果您有一个类实例,它将接收回调,然后将该委托分配给该类中的一个字段。


希望这会有所帮助。 />
-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Wilfried Mestdagh" < Wi ************** @ discussion.microsoft.com写在

消息新闻:8A ************** ******************** @ microsof t.com ...
Wilfried,

Just assign the delegate reference to a field in a class that has the
same lifetime as the callback. So if you have a class instance which will
receive the callback, then assign the delegate to a field on the class.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Wilfried Mestdagh" <Wi**************@discussions.microsoft.comwrote in
message news:8A**********************************@microsof t.com...




我有一个使用DLL和回调的应用程序。它生成随机的

错误对垃圾收集的代理进行回调。


我发现一些文章指向代表的指针必须有一个

的生命周期参考,这样它就不会被垃圾收集。但我不能

找到

任何一个例子如何做到这一点。


有人可以帮忙吗?
< br $> b $ b -

rgds,Wilfried
http://www.mestdagh.biz






我不知道但是我不要低估如何以及在哪里。


我有以下内容:


公共课Api

{

公共Api(控制控制)

{

owner = control;

ApiDll.Start();

ApiDll.SetLoggedOn(cbLoggedOn);

// cb ...是这个类中的回调函数

// Set LoggedOn是放置指针的过程在DLL中

//等...


然后以主要形式:


公共分部类:表格

{

私人Api api;


公共主要()

{

InitializeComponent();

api = new Ap我(这);

api.OnLoggedOn = = api_OnLoggedOn;

//等等。


然后在DLL声明中:


公共静态类ApiDll

{

私有const字符串dllName =" SmsComfortAPI.dll" ;;


public delegate void OnLoggedOn();

//等...


[DllImport(dllName)]

public static extern void SetLoggedOn(OnLoggedOn cbLoggedOn);

//等等......

我应该在哪里以及如何申报终生因素?


-

rgds,Wilfried
http://www.mestdagh.biz


Wilfried,


你从哪里得到cb ?如果它是你的Api课程的成员,

那么问题是正在收集课程(不仅仅是

代表)。

你需要调用一个函数来取消注册回调,这样它就不会尝试回调你的函数了。要么是这样,要么你需要一个

静态方法并将委托传递给它,然后在类

注册回调。

-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Wilfried Mestdagh" < Wi ************** @ discussion.microsoft.com写在

消息新闻:A0 ************** ******************** @ microsof t.com ...
Wilfried,

Where are you getting cb from? If it is a member in your Api class,
then the problem is that the class is being collected (not just the
delegate).

You need to call a function to unregister the callback so that it
doesn''t try and call your function back. Either that, or you need to have a
static method and pass a delegate to that, and then register with the class
for the callback.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Wilfried Mestdagh" <Wi**************@discussions.microsoft.comwrote in
message news:A0**********************************@microsof t.com...




我不知道但是我不知道如何以及在哪里。


我有以下内容:


公共类Api

{

公共Api(控制控制)

{

owner = control;

ApiDll.Start();

ApiDll.SetLoggedOn(cbLoggedOn);

// cb ...是此类中的回调函数

//设置LoggedOn是将指针放在

DLL中的过程

//等等......


然后主要形式:


公共部分课程主要:表格

{

私人Api api;

public Main()

{

InitializeComponent();

api = new Api(this);

api.OnLoggedOn + = api_OnLoggedOn;

//等等。


然后进入DLL声明:


公共静态类ApiDll

{

私有const字符串dllName =" SmsComfortAPI.dll" ;;


public delegate void OnLoggedOn();

//等等......


[DllImport(dllName)]

public static extern void SetLoggedOn(OnLoggedOn cbLoggedOn);

// etc ...


我应该在哪里以及如何申报一生的态度?


-

rgds,Wilfried
http://www.mestdagh.biz



这篇关于回调垃圾收集委托错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆