C# 事件注册过程中,是否会导致程序死掉

laorer 2009-10-30 05:43:13
class CA
{
public delegate void IqHandler(Object o);
public event IqHandler OnIq;
}

class CB
{
private void RegisterOnIq()
{
CA ca = new CA();
ca.OnIq += new CA.IqHandler(ca_OnIq);
}

void ca_OnIq(object o)
{
Console.WriteLine(" CB, onIq");
}
}

C# 事件注册过程中,是否会导致程序死掉

在 ca.OnIq += new CA.IqHandler(ca_OnIq);过程中,是否会导致程序死掉

CsToD 提到可能会是因为有非托管的对象导致GC提前回收了委托实例 new CA.IqHandler(ca_OnIq)这个对象

建议用 对象字段来保证其不会GC随机回收委托实例
我试了之后程序还是会随机的死掉

查了下 Microsoft.NET框架程序设计(11章) ,C#事件的注册过程是线程同步的,不清楚是不是和这个有关系,我不知如何来确认……
再找了下资料,又提到了消息循环,一点概念都没有……

大家看看,会是什么原因引起的?

可以看下这个帖子: http://topic.csdn.net/u/20091030/09/e0caa35b-0188-4d33-ad75-77650e7d810c.html
...全文
95 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzhdim 2009-10-31
  • 打赏
  • 举报
回复
支持6楼。不过没有把委托的典型应用描述出来。建议LZ先把委托的典型应用搞清楚。。。
龙宜坡 2009-10-31
  • 打赏
  • 举报
回复
单纯从你贴的代码来看,看不出问题所在!

我认为是事件发布方存在问题!

看看MSDN上的示例代码:如何:发布符合 .NET Framework 准则的事件(C# 编程指南)
代码如下:
namespace DotNetEvents
{
using System;
using System.Collections.Generic;

// Define a class to hold custom event info
public class CustomEventArgs : EventArgs
{
public CustomEventArgs(string s)
{
message = s;
}
private string message;

public string Message
{
get { return message; }
set { message = value; }
}
}

// Class that publishes an event
class Publisher
{

// Declare the event using EventHandler<T>
public event EventHandler<CustomEventArgs> RaiseCustomEvent;

public void DoSomething()
{
// Write some code that does something useful here
// then raise the event. You can also raise an event
// before you execute a block of code.
OnRaiseCustomEvent(new CustomEventArgs("Did something"));

}

// Wrap event invocations inside a protected virtual method
// to allow derived classes to override the event invocation behavior
protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
{
// Make a temporary copy of the event to avoid possibility of
// a race condition if the last subscriber unsubscribes
// immediately after the null check and before the event is raised.
EventHandler<CustomEventArgs> handler = RaiseCustomEvent;

// Event will be null if there are no subscribers
if (handler != null)//////////////关键在这里,有没有对委托的实例进行判断
{
// Format the string to send inside the CustomEventArgs parameter
e.Message += String.Format(" at {0}", DateTime.Now.ToString());

// Use the () operator to raise the event.
handler(this, e);
}
}
}

//Class that subscribes to an event
class Subscriber
{
private string id;
public Subscriber(string ID, Publisher pub)
{
id = ID;
// Subscribe to the event using C# 2.0 syntax
pub.RaiseCustomEvent += HandleCustomEvent;
}

// Define what actions to take when the event is raised.
void HandleCustomEvent(object sender, CustomEventArgs e)
{
Console.WriteLine(id + " received this message: {0}", e.Message);
}
}

class Program
{
static void Main(string[] args)
{
Publisher pub = new Publisher();
Subscriber sub1 = new Subscriber("sub1", pub);
Subscriber sub2 = new Subscriber("sub2", pub);

// Call the method that raises the event.
pub.DoSomething();

// Keep the console window open
Console.WriteLine("Press Enter to close this window.");
Console.ReadLine();

}
}
}



请把你跟踪到的错误信息贴出来看看!
staticuser 2009-10-31
  • 打赏
  • 举报
回复
这么强?
laorer 2009-10-31
  • 打赏
  • 举报
回复
先谢谢各位了,
没有 出现任何异常,至少我没抓到

另外,他是在注册时发生的, 不是在事件触发时发生的
cuike519 2009-10-30
  • 打赏
  • 举报
回复
下载个windbg,

用脚本:adplus -crash -p ProcessID -quiet -o c:\dumps

程序在Crash的时候会出现xxx.dump,分析一下这个dump就知道为什么崩溃了。
laorer 2009-10-30
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yuxuanji 的回复:]
你这例子必然不会死掉
如果死掉一般是用到的非托管资源被GC释放掉了
[/Quote]

如果用到了非托管资源的话,注册时会不会报异常?

我碰到的没有报异常,断点调试的时候,如果正常的话,则过完所有的断点才会显示界面,如果不正常的话,就会在注册这段,直接显示界面,但界面动不了,而且界面上的控件是反白的
LutzMark 2009-10-30
  • 打赏
  • 举报
回复
你这例子必然不会死掉
如果死掉一般是用到的非托管资源被GC释放掉了
lee_b 2009-10-30
  • 打赏
  • 举报
回复
UP,学习。顺便抢个沙发。。接点分。。

110,502

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧