Sirwan Afifi
Sirwan Afifi

Reputation: 10824

Silverlight: Occurring click event several times

I'm using SignalR in WCF ria service (Silverlight client). below is my configurations for establish connection to my Hub:

private void btn_click(object sender, RoutedEventArgs e)
{
            var hubConnection = new HubConnection(url: "http://10.1.0.5:2096/signalr/");
            var chat = hubConnection.CreateHubProxy(hubName: "chat");
            chat.On<string>("hello", msg => System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(msg)));
            hubConnection.Start().Wait();
            chat.Invoke<string>("sendMessage", "Hello!");
}

Hub:

[HubName("chat")]
public class ChatHub : Hub
{
        public void SendMessage(string message)
        {
            Clients.All.hello(message);
        }
}

connection is started successfully, But each time I click on the button, it fires several times. for example first time it fires once, second time it fires twice and ....
Any idea?

Upvotes: 0

Views: 45

Answers (1)

Peter
Peter

Reputation: 27944

Your message is send once, however you register the event handler on each click. Move this out of the btn click event.

var chat = hubConnection.CreateHubProxy(hubName: "chat");
chat.On<string>("hello", msg => System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => MessageBox.Show(msg)));

Upvotes: 1

Related Questions