Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - SignalR How can I remove my event handler

I am a C# newbie trying to implement SignalR in my Xamarin IOS app.

My code is quite simple:

 _connection = new Microsoft.AspNet.SignalR.Client.Hubs.HubConnection (Common.signalRAddress);

feedHub = _connection.CreateHubProxy ("feedHub");

_connection.Received += data =>  { OnReceiveData (data); };

_connection.Start ();   

my question is how can I remove my delegate? Is it enough to write?

_connection.Received -= data =>  { OnReceiveData (data); };

Any help would be really appreciated.

like image 765
vassilag Avatar asked Dec 01 '22 21:12

vassilag


1 Answers

You're using a hub, why not use the built in on/off for method invocations?

aka:

var doSomething = feeHub.On<int>("doSomething", val => {
    // Do something with int val
});

Then to remove it you can do:

doSomething.Dispose();

If you truly want to listen to ALL data that flows through the hub then using Received is the correct approach and @Dracanus' answer will work.

like image 53
N. Taylor Mullen Avatar answered Dec 20 '22 15:12

N. Taylor Mullen