为优先事件处理程序 [英] Prioritising Event Handlers

查看:116
本文介绍了为优先事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,其中我处理一个事件的两倍。不过,我总是希望确保 mynewclass 总是第一,然后处理该事件的本地事件处理函数code类火灾。我理解的MyClass的事件应该首先开火,因为这是在第一个,但因为线程入队正在发生,我认为它的时间太长和做一些在 myhandleeventlocal 之前,我希望它这样做。什么办法可以等待它的发生吗?

I have the following code where I am handling an event twice. However I always want to ensure that mynewclass always handles the event first and then the local event handler code fires. I understand the MyClass event should fire first as that is the one created first but because the thread and enqueuing is taking place, I think its taking too long and its doing something in myhandleeventlocal before I want it to do that. Any way I can wait for it to happen?

    public MyMainClass
    {

    private MyMethod()
    {
        MyClass mynewclass = new MyClass();
        mynewclass.myObject += MyHandler(myhandleventlocal);
        mynewclass.loadedevent += EventHandler(loadedevent)
    }

    private void myhandleventlocal()
    {

             //do stuff

    }

    private void loadedevent()
    {
         //do some stuff
     }

    }

    public MyClass
    {
         public MyObject myObject;
         public event loadedevent;
         public MyClass()
         {
               myObject = new MyObject();
               myObject += MyHandler(myhandlevent);

         }

         private void myhandlevent(long value, string detail)
         {

             //Start a thread
             //Enqueue value and detail
             //On seperate thread dequeue value and process it
             //Raise loadedevent event

         }

    }

更新:我已经更新了我的问题,code来说明问题

推荐答案

默认的事件处理程序被调用的顺序添加它们,所以,如果你总是在你想要的顺序添加处理程序他们开火那么它应该工作。

By default the event handlers are called in the order you add them, so if you always add the handlers in the order you want them to fire then it should work.

从乔恩斯基特对事件和委托文章:

[...]代表额外都加入并从列表的末尾[...]

[...] extra delegates are both added to and removed from the end of the list [...]

请注意:您可以通过更改覆盖事件的默认行为添加删除操作上的事件指定一些其他的行为。然后,您可以让您的事件处理程序中,你管理自己的清单,并办理点火顺序根据什么规则,你喜欢的。

Note: You can override the default behaviour of events by changing the add and remove operations on your event to specify some other behaviour. You can then keep your event handlers in a list that you manage yourself and handle the firing order based on whatever rules you like.

这篇关于为优先事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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