the Form stop to respond
-
Hi gurus, I am currently testing the TcpListener and TcpClient classes from System.Net.Socket to try to send data from a pc to another. I have inserted a static control that displays the status of the connection. I do the following code:
... Int32 port=Int32.Parse(txtPort.Text); NetworkStream stream=null; Socket socket=null; m_server=new TcpListener(IPAddress.Any, port); m_server.Start(); while (true) { lblStatus.Text="Waiting for a connection..."; Debug.WriteLine(lblStatus.Text); socket=m_server.AcceptSocket(); ...
When I check the output I can see the message "Waiting for connection..." (that's what I wanted) but the form seems to be completely frozen once the call tosocket=m_server.AcceptSocket()
is done. Should I write this function in a separate thread? Then the interface could answer to the OnPaint events like in C++? If I have to write a thread for what I'm doing, where I can I get a sample code for it? Thanks for the help. Best regards. There is no spoon. -
Hi gurus, I am currently testing the TcpListener and TcpClient classes from System.Net.Socket to try to send data from a pc to another. I have inserted a static control that displays the status of the connection. I do the following code:
... Int32 port=Int32.Parse(txtPort.Text); NetworkStream stream=null; Socket socket=null; m_server=new TcpListener(IPAddress.Any, port); m_server.Start(); while (true) { lblStatus.Text="Waiting for a connection..."; Debug.WriteLine(lblStatus.Text); socket=m_server.AcceptSocket(); ...
When I check the output I can see the message "Waiting for connection..." (that's what I wanted) but the form seems to be completely frozen once the call tosocket=m_server.AcceptSocket()
is done. Should I write this function in a separate thread? Then the interface could answer to the OnPaint events like in C++? If I have to write a thread for what I'm doing, where I can I get a sample code for it? Thanks for the help. Best regards. There is no spoon.There's plenty of examples of threading socket connections here on CodeProject. When you get and set properties on a control or form, however, you must invoke the call so that it executes on the main UI thread otherwise you'll run into problems. See
Control.Invoke
andControl.InvokeRequired
for more information and an example of how to use them.Microsoft MVP, Visual C# My Articles
-
Hi gurus, I am currently testing the TcpListener and TcpClient classes from System.Net.Socket to try to send data from a pc to another. I have inserted a static control that displays the status of the connection. I do the following code:
... Int32 port=Int32.Parse(txtPort.Text); NetworkStream stream=null; Socket socket=null; m_server=new TcpListener(IPAddress.Any, port); m_server.Start(); while (true) { lblStatus.Text="Waiting for a connection..."; Debug.WriteLine(lblStatus.Text); socket=m_server.AcceptSocket(); ...
When I check the output I can see the message "Waiting for connection..." (that's what I wanted) but the form seems to be completely frozen once the call tosocket=m_server.AcceptSocket()
is done. Should I write this function in a separate thread? Then the interface could answer to the OnPaint events like in C++? If I have to write a thread for what I'm doing, where I can I get a sample code for it? Thanks for the help. Best regards. There is no spoon.I had the same problem and used seperate threads for waiting on a connection, and sending and receiving data. This frees up the main thread so the UI doesn't appear to stop responding. This link - http://staff.develop.com/woodring/dotnet/#socketsamp[^] - posted in reply to an article gives a good example, which got me going. Barry
-
I had the same problem and used seperate threads for waiting on a connection, and sending and receiving data. This frees up the main thread so the UI doesn't appear to stop responding. This link - http://staff.develop.com/woodring/dotnet/#socketsamp[^] - posted in reply to an article gives a good example, which got me going. Barry
-
There's plenty of examples of threading socket connections here on CodeProject. When you get and set properties on a control or form, however, you must invoke the call so that it executes on the main UI thread otherwise you'll run into problems. See
Control.Invoke
andControl.InvokeRequired
for more information and an example of how to use them.Microsoft MVP, Visual C# My Articles