本文介绍了我使用正确的方法来监视我想创建句柄时,执行的任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有用于创建一个事件处理程序,退订本身就是一个普遍接受的最佳实践

Is there a generally-accepted best practice for creating an event handler that unsubscribes itself?

比如说,我想出的第一件事是这样的:

E.g., the first thing I came up with is something like:

// Foo.cs

// ...
Bar bar = new Bar(/* add'l req'd state */);
EventHandler handler = new EventHandler(bar.HandlerMethod);

bar.HandlerToUnsubscribe = handler;

eventSource.EventName += handler;
// ...





// Bar.cs

class Bar
{
    /* add'l req'd state */

    // .ctor

    public EventHandler HandlerToUnsubscribe { get; set; }

    public void HandlerMethod(object sender, EventArgs args)
    {
        // Do what must be done w/ add'l req'd state
        ((EventSourceType)sender).EventName -= this.HandlerToUnsubscribe;
    }
}





如果说这种感觉的hackish /坏是轻描淡写。它紧密结合了时间依赖性( HandlerToUnsubscribe 必须在明确的时间分配的精确值)。我觉得我必须打在这种情况下complicator的一部分---有一些愚蠢的事或简单的我失踪

To say that this feels hackish/bad is an understatement. It's tightly coupled with a temporal dependency (HandlerToUnsubscribe must be assigned the exact value at the exact right time). I feel like I must be playing the part of a complicator in this case--- is there something stupid or simple that I'm missing?

我创建一个用户界面,并在的WinForms专有指挥基础设施(使用有用的ICommand 在System.Windows之间的绑定。输入)。结合基础设施建设的一个方面是谁创建UI命令组件之间的绑定(如工具栏按钮或菜单项),用户必须听指挥的 CanExecuteChanged 活动的选项然后根据that--通常设置Enabled属性的用户界面的状态更新真正

I'm creating a binding between the UI and a proprietary commanding infrastructure in Winforms (using the useful ICommand in System.Windows.Input). One aspect of the binding infrastructure is that users who create a binding between a UI command component (like a toolbar button or menu item) have the option to listen to the command's CanExecuteChanged event and then update the UI's state based on that-- typically setting the Enabled property to true or false.

该技术通常工作得很好,但有办法的事件之前,已经创造了一个UI组件的手柄被解雇。我想,以保证所提供的处理程序无法运行,除非手柄已创建。 Resultantly,我正在考虑提供一个通用助手类(酒吧),将有助于实现。 酒吧的目标是要查看是否将适当的处理存在。如果是这样,太好了!如果不是,它会认购,最终使创建句柄时所提供的处理程序得到运行,以相应的 IsHandleCreated 事件。 (这是很重要的B / C客户端可能会设置其绑定在UI的.ctor,手柄存在了。)我想这个订阅是完全透明的,但是,所以我也希望每个事件处理程序自动将自己从退订 IsHandleCreated 一旦它的运行结束。

The technique generally works quite well, but there are ways for the event to be fired prior to a ui component's handle having been created. I'm trying to guarantee that the provided handler isn't run unless the handle has been created. Resultantly, I'm considering providing a general helper class ("Bar") that will aid implementation. The goal of Bar is to check to see if the appropriate handle exists. If so, great! If not, it will subscribe to appropriate IsHandleCreated event so that the supplied handlers get run when the handle eventually is created. (This is important b/c the client may set their bindings in the UI's .ctor, before a handle exists.) I want this subscription to be completely transparent, however, and so I also want each event handler to automatically unsubscribe itself from IsHandleCreated once it's finished running.

我还在一个地步,我想,如果这弄清楚是一个好主意,所以我还没有广义的概念yet--我只有在这种情况下,直接实现对它向ToolStripItems验证这个想法是合理的。我不把它卖给然而,虽然。

I'm still at a point where I'm trying to figure out if this is a good idea, so I haven't generalized the concept yet-- I've only implemented it directly against ToolStripItems in this case to verify that the idea is sound. I'm not sold on it yet, though.

据我所知,我也有简单的强制,一旦UI的句柄已创建绑定只能创建选项在一个形式(如)的onload事件。我知道,能正常工作,我已经在过去做了。我想看看我是否可以缓解在这种情况下该特殊要求虽然。如果它甚至实际的。

I understand that I also have the option of simply mandating that bindings can only be created once the UI's handle has been created, in the OnLoad event of a form (e.g.). I know that can work, I've done it in the past. I'd like to see if I can ease that particular requirement in this case though. If it's even practical.

推荐答案

格雷格,

您有哪些不是的观察者模式,而是一个消息队列。所以,你只是使用你试图解决问题的错误的设计模式。

What you have is not an observer pattern, but rather a message queue. So you're just using the wrong design pattern for the problem you're trying to solve.

它很容易使用从头实现自己的消息队列队列{{行动}对象} ,其中对象入队自己,你只需出队的项目,如你调用它们。

Its easy enough to implement your own message queue from scratch using a Queue{Action{object}}, where objects enqueue themselves, and you simply dequeue items as you invoke them.

这篇关于我使用正确的方法来监视我想创建句柄时,执行的任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 15:56