繁体   English   中英

当发生某些事情时,我如何将事件触发到另一个类?

[英]how can i fire event to another class when something happen?

class B
{
    int x =0;
    int loop=0;

    public void count()
    {
        while (loop=0)
        {
            Thread.Sleep(5000);
            x+=5`enter code here`;
            // fire event to another class 
        }
    }
}

我试图在整数 x 达到 15 时触发另一个类的事件。

当我的方法达到另一个类的 15 号时,我如何触发事件?

创建一个事件

public delegate void YourEvent(object sender, YourEventArgs e);
public event YourEvent YourHandler;

然后当 x 达到 15 时调用

YourHandler(this, new YourEventArgs());

注意 YourEventArgs 只是一个具有您想要放入的任何属性的类

在将获得结果的类上,只需为此实例创建一个处理程序,就可以了。 希望这可以帮助。

B.YourHandler += new YourEvent(A_Handler);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM