简体   繁体   English

优先处理事件处理程序

[英]Prioritising Event Handlers

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. 但是,我总是希望确保mynewclass始终首先处理事件,然后触发本地事件处理程序代码。 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. 我理解MyClass事件应首先触发,因为它是首先创建的事件,但因为线程和入队正在发生,我认为它花了太长时间并且在我想要它之前在myhandleeventlocal做了一些事情。 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

         }

    }

UPDATE: I have updated my question and code to demonstrate the problem. 更新:我已更新我的问题和代码以证明问题。

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. 默认情况下 ,事件处理程序按您添加它们的顺序调用,因此如果您始终按照您希望它们触发的顺序添加处理程序,那么它应该可以工作。

From Jon Skeet's article on events and delegates : 来自Jon Skeet关于活动和代表的文章:

[...] 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. 注意:您可以通过更改事件的addremove操作来覆盖事件的默认行为,以指定其他一些行为。 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. 然后,您可以将事件处理程序保存在您自己管理的列表中,并根据您喜欢的规则处理触发顺序。

如果您不能保证订单将添加事件处理程序,只需为mynewclass添加一个,然后在该代码中调用其他代码。

Since event handlers are called in the order you add them, based on the code I see in your question, you can't make mynewclass's handler be called first. 由于事件处理程序是按照您添加它们的顺序调用的,因此根据我在您的问题中看到的代码,您无法首先调用mynewclass的处理程序。 The event handler that MyClass creates is always added first. 始终首先添加MyClass创建的事件处理程序。

One solution would be to control priority for the event handlers. 一种解决方案是控制事件处理程序的优先级。 Instead of using the builtin event handler +=/-= operators, you would instead have methods for adding and removing events where you could specify ordering explicitly. 您可以使用添加和删除事件的方法来代替使用内置事件处理程序+ = / - =运算符,您可以在其中明确指定排序。 That way, if a class knows it needs to handle the event first, it could ask for such. 这样,如果一个类知道它需要首先处理事件,它可以要求这样做。 Be careful, though, because you could easily run into a situation where multiple classes are each insisting that they handle the event first. 但要小心,因为您可能很容易遇到多个类都坚持要先处理事件的情况。

Here is some quick and dirty code to get you started: 这里有一些快速而又脏的代码可以帮助您入门:

class MyClass {
    private LinkedList<MyEventHandler> eventHandlers;
    public enum Ordering { First, Last, ... };
    public void AddHandler(MyEventHandler handler, Ordering order) {
        switch(order) {
            case Ordering.First:
                eventHandlers.AddFirst(handler);
                break;
            // fill in other cases here...
        }
    }
    public void RaiseEvent() {
        // call handlers in order
        foreach(MyEventHandler handler in eventHandlers)
            eventHandler();
    }
}

Referring to siride solution, you can also implement your handlers and decide the position that way. 参考siride解决方案,您还可以实现处理程序并以此方式确定位置。 Like inverting the order (always add at the begin) or add some logic. 像反转顺序(总是在开头添加)或添加一些逻辑。

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

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