我想在发生陷阱时向任何订户通知警报消息.
我通过委托方法(myDelegate del)制作的代码很好.
我的问题是......
我想知道是否值得使用Eventhandler更好的代理?在我的情况下,不确定委托和事件之间的区别是什么?
通知(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)