Event handler is executed more than once

When I use this piece of code, MessageOutput.Text is set 2 times, which means the code is executed twice, since I do not set MessageOutput.Text elsewhere. Whenever I receive a new message, this method is called and it should update the user interface. 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");
        });
    }
0
source share
2 answers

Why is this happening and how can I fix it?

The most likely reason is that there are several virtual network switches in your system .

, Windows 10 , - > - > . enter image description here

:

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

( : 169.254.146.116, : 22113): "" ( : 172.16.80.1, : 22113): "" ( : 10.168.177.14, : 22113): ""

ipconfig /all cmd, IPv4 :

enter image description here

+1

, .

listenerSocket.MessageReceived += MessageReceived;

, . 2 . , . , , . , !!!

-1

Source: https://habr.com/ru/post/1655810/


All Articles