D
D
DerilH2021-11-01 14:45:15
WPF
DerilH, 2021-11-01 14:45:15

Socket breaks the connection when moving the mouse, how to solve?

Hello! There is such code (WPF project):

public partial class MainWindow : Window
    {
        IPAddress iP;
        int port = 8005;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void EnterIP_Click(object sender, RoutedEventArgs e)
        {
            iP = IPAddress.Parse(IPAddressB.Text);

            ConnectToServer();
        }

        void  ConnectToServer() 
        {
            new Thread(() => {
                try
                {

                    Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect(new IPEndPoint(iP, port));
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    MessageBox.Show(ex.Message);
                }
            }).Start();
        }
    }

When I move the mouse in the application window, the socket disconnects, if I do something outside the window, the socket works. I left the PC for 15 minutes and there were no problems, but as I moved the mouse, the connection was interrupted. I just can't understand how the mouse affects the Socket, which is also in a different thread. Please help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
none7, 2021-11-01
@DerilH

It is in this version that the flow ends immediately after the connection. What happens to the object (Socket) the reference to which is lost? It becomes garbage and is destroyed when the garbage collector deigns to run. In your case, it is triggered by actions with WPF.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question