CaTx
CaTx

Reputation: 1481

TCP/IP causing WinForm slow response

I have a C# WinForm running a TCP/IP server and a client. when clicking on a button, a TCP client is instantiated to transmit a message, then it is closed. the server captures the message, and show it on a message box.

question:
my WinForm seems to be interrupted. it does not always respond to my click (which instantiates the client message transmit method). I suppose this is due to the server thread? if so, what can I do to fix this interruption.

Upvotes: 0

Views: 301

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500035

It sounds like your networking is occurring in the UI thread. That's a big problem: the UI thread is meant to stay available to respond to user events.

You should put your networking onto a separate thread, or use an asynchronous API. Don't forget that you can only update your UI from the UI thread though - so you'd typically either use BackgroundWorker to report progress/completion on the UI thread, or use Control.Invoke/Control.BeginInvoke to execute a delegate on the UI thread. You can read up on threading in various books and tutorials - I like Joe Albahari's tutorial.

If you're using .NET 4.5 / C# 5, you can use asynchronous calls to make your life a lot easier - but if you're using an earlier version of .NET, using a separate thread is probably going to be simpler.

Upvotes: 1

Related Questions