0

When I use this snippet of code The MessageOutput.Text is set 2 times, which means that the code is executed twice since I don't set the MessageOutput.Text anywhere else. Whenever I get a new message, this method is called and it is supposed to update the UI. Why is this happening and how can I fix it?

 async void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
    {
        uint stringLength = eventArguments.GetDataReader().UnconsumedBufferLength;
        string receivedMessage = eventArguments.GetDataReader().ReadString(stringLength);

        await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            MessageOutput.Text += (receivedMessage + "\n");
        });
    }
pquest
  • 3,151
  • 3
  • 27
  • 40
Stefan
  • 188
  • 2
  • 19
  • are you sure the method isn't being called twice? – pquest Jul 31 '16 at 13:26
  • This method is called when i receive a new message `listenerSocket.MessageReceived += MessageReceived;` and in this case it displays the received message twice. – Stefan Jul 31 '16 at 13:33
  • That does not change my question at all. Put a break point on the first line of `MessageReceived` and see if you hit it twice – pquest Jul 31 '16 at 13:34
  • Yes, it is called twice, but I have no clue why. – Stefan Jul 31 '16 at 13:38
  • please edit your question and add the code where you add the event handler. If there is more than one place where you do so, please show them all. – pquest Jul 31 '16 at 13:39
  • [Here](http://stackoverflow.com/questions/38679646/uwp-datagramsocket-multicast) I have posted a different question with the full code, so maybe you can take a look at it. – Stefan Jul 31 '16 at 13:42
  • I also suspect the event handler is registered twice somehow, the code that contains the handler registration would be very helpful to see. – Martin Zikmund Jul 31 '16 at 13:42
  • I agree with @MZetko that this is likely the issue. Is `StartListener_Click` being called twice? – pquest Jul 31 '16 at 13:48
  • I just checked and the `StartListener_Click` is called only once. – Stefan Jul 31 '16 at 13:52
  • somewhere in your code that handler is being assigned a second time. You are going to have to hunt it down to fix the issue. – pquest Jul 31 '16 at 13:58
  • This is what i get when I click on Find all references `[Client001] void MainPage.MessageReceivedEvent(DatagramSocket, DatagramSocketMessageReceivedEventArgs) (1 reference) Client001\MainPage.xaml\MainPage.xaml.cs - (88, 47) : listenerSocket.MessageReceived += MessageReceivedEvent;` – Stefan Jul 31 '16 at 14:01
  • @Stefan Based on your other post, I see that you are doing multicast. By any chance, do you have more than one network connection (e.g. a wired and wireless connection)? – seairth Jul 31 '16 at 14:02
  • Currently I am on my laptop and I am using the Wi-Fi only. In my other post you can find the whole code, so maybe if you have 5 minutes to spare you can try it on your computer. If you decide to try it, since this is UWP you will need Windows 10, VS2015 and Developer settings enabled (which I am sure you have all). – Stefan Jul 31 '16 at 14:03
  • @Stefan Have you tried running the app from two separate machines? – seairth Jul 31 '16 at 16:28
  • No, I haven't tried that, since I only have one machine with Windows 10. – Stefan Jul 31 '16 at 18:44

2 Answers2

1

Why is this happening and how can I fix it

The most possible reason is, there are several virtual network switches in your system.

For example, there are three virtual network switches in my Windows 10, just go to Control Panel->Network and Internet->Network Connections. enter image description here

I will get three messages at the same time:

Debug.WriteLine("Received data from remote peer (Remote Address: " +
                    eventArguments.RemoteAddress.CanonicalName + ", Remote Port: " +
                    eventArguments.RemotePort + "): \"" + receivedMessage + "\"");

Received data from remote peer (Remote Address: 169.254.146.116, Remote Port: 22113): "Hello" Received data from remote peer (Remote Address: 172.16.80.1, Remote Port: 22113): "Hello" Received data from remote peer (Remote Address: 10.168.177.14, Remote Port: 22113): "Hello"

Please type ipconfig /all in cmd to check the IPv4 addresses for these switches:

enter image description here

Franklin Chen - MSFT
  • 4,845
  • 2
  • 17
  • 28
  • Thank you so much, all I had to do was disable my "vEthernet (Internal Ethernet Port Windows Phone Emulator Internal Switch)". – Stefan Aug 04 '16 at 12:01
-1

I suspect the problem is in this line.

listenerSocket.MessageReceived += MessageReceived;

Please add the code where you have binded this event handler. Because exceptionally this line executed 2 times. So that's why your event handler gets executed twice.Hence you have to figure it out why this event is binded twice and your problem would be resolved. Hope this help !!!

Lalit Rajput
  • 271
  • 2
  • 5
  • 23