小编Hsu*_*eng的帖子

C#delegate vs eventHandler

我想在发生陷阱时向任何订户通知警报消息.

我通过委托方法(myDelegate del)制作的代码很好.

我的问题是......

  1. 我想知道是否值得使用Eventhandler更好的代理?在我的情况下,不确定委托和事件之间的区别是什么?

  2. 通知(trapinfo t),这就是我在这里所做的,以获取陷阱信息.但似乎不是一个好主意.阅读一些介绍传递委托对象的在线教程课程,是否适合我的情况?我该怎么办?有什么建议?

非常感谢 :)

我的代码:

public class trapinfo
    {
        public string info;
        public string ip;
        public string cause;
    }

    public class trap
    {
        public delegate void myDelegate(trapinfo t);
        public myDelegate del;

        trapinfo info = new trapinfo();

        public void run()
        {
            //While(true) 
            // If a trap occurred, notify the subscriber
            for (; ; )
            {
                Thread.Sleep(500);
                foreach (myDelegate d in del.GetInvocationList())
                {
                    info.cause = "Shut Down";
                    info.ip = "192.168.0.1";
                    info.info = "Test";
                    d.Invoke(info);
                }
            }
        } …
Run Code Online (Sandbox Code Playgroud)

c# delegates event-handling observer-pattern

20
推荐指数
2
解决办法
5万
查看次数

标签 统计

c# ×1

delegates ×1

event-handling ×1

observer-pattern ×1