简体   繁体   English

删除匿名事件处理程序

[英]Removing anonymous event handler

I have the following code where SprintServiceClient is a reference to a WCF Service- 我有以下代码,其中SprintServiceClient是对WCF服务的引用 -

public class OnlineService
{
    private SprintServiceClient _client;
    public OnlineService()
    {
        _client = new SprintServiceClient();
    }

    public void AddMemberToTeam(MemberModel user, int projectId, Action<int> callback)
    {
        _client.AddMemberToTeamCompleted += (s, e) => callback(e.Result);
        _client.AddMemberToTeamAsync(user.ToUser(), projectId);
    }
}

the problem is that every time AddMemberToTeam is called it adds another callback to client.AddMemberToTeamCompleted 问题是,每次调用AddMemberToTeam时,它都会向client.AddMemberToTeamCompleted添加另一个回调。

ie the first time AddMemberToTeam is called the callback is called once, the second time AddMemberToTeam is called the callback is called twice ect. 即第一次调用AddMemberToTeam时,调用一次回调,第二次调用AddMemberToTeam,调用两次ect回调。

Is there any way to remove the eventhandler from AddMemberToTeamCompleted once the eventhandler has been called or use another method which takes in the callback? 有没有办法在调用eventhandler后从AddMemberToTeamCompleted中删除eventhandler或使用另一个接受回调的方法?

You can refer to your anonymous method from inside itself as long as you assign a delegate to a variable first: 只要先将委托分配给变量,就可以从内部引用匿名方法:

EventHandler<SomeEventArgs> handler = null;
handler = (s, e) =>
    {
        _client.AddMemberToTeamCompleted -= handler;
        callback(e.Result);
    };

_client.AddMemberToTeamCompleted += handler;

Note that you need to declare the variable and assign it separately or the compiler will deem it uninitialized when you come to use it inside the method body. 请注意,您需要声明变量并单独指定它,否则当您在方法体内使用它时,编译器会认为它未初始化。

The trick to making a self-unsubscribing event-handler is to capture the handler itself so you can use it in a -= . 制作自取消订阅的事件处理程序的技巧是捕获处理程序本身,以便您可以在-=使用它。 There is a problem of declaration and definite assignment , though; 但是,存在着声明明确指配的问题; so we can't do something like: 所以我们做不到这样的事情:

EventHandler handler = (s, e) => {
    callback(e.Result);
    _client.AddMemberToTeamCompleted -= handler; // <===== not yet defined     
};

So instead we initialize to null first, so the declaration is before the usage, and it has a known value ( null ) before first used: 因此我们首先将null初始化为null ,因此声明使用之前 ,并且在首次使用之前它具有已知值( null ):

EventHandler handler = null;
handler = (s, e) => {
    callback(e.Result);
    _client.AddMemberToTeamCompleted -= handler;        
};
_client.AddMemberToTeamCompleted += handler;

No there is no way, 不,没有办法,

Apparantly Tim and Marc have another nice solution 显然蒂姆和马克有另一个很好的解决方案

But you can always just name them, and do the -= on the named eventhandler on this method ;) 但是你总是可以命名它们,并在这个方法上对命名的eventhandler执行-= ;)

Guessing your event: 猜猜你的活动:

_client.AddMemberToTeamCompleted += OnAddMemberToTeamCompleted;

and

public void OnAddMemberToTeamCompleted(object sender, EventArgs args)
{
    _client.AddMemberToTeamCompleted -= OnAddMemberToTeamCompleted;
    callback(e.Result)
}

Next problem is getting this callback in your listener. 下一个问题是在您的侦听器中获取此回调。 Perhaps putting it on a Property in the EventArgs (but that feels kinda dirty, I agree) 也许把它放在EventArgs的一个属性上(但感觉有点脏,我同意)

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

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