views:

308

answers:

3

Currently my GUI is Freezing when I try to display data I read from my socket. It's not freezing when i display one, or two messages, only when I receive many messages(2-3 per sec). I am using async socket(non blocking). I guess it's related to how I display the data to the gui. Code:

public class Globals
{

     public static MainDialog myDialog;
}

public partial class MainDialog : Form
{
    public MainDialog()
    {
        InitializeComponent();
        Globals.myDialog = this;

    }
    public void sendText(string text)
    {
         logBox.AppendText(text);
         logBox.AppendText("\n");
    }
}

Then I simply call Globals.myDialog.sendText(..) from my network class. As I said it works fine, but not when I receive many messages at once.

Anyone have an Idea?

+1  A: 

Looking at the code, my guess is that your network class that reads the data from the socket is not on a seperate thread. I would use the BackgroundWorker class to run the socket data on, then call ProgressChanged to bring data back to your GUI. Here is a good example how to setup the BackgroundWorker to run a job and upate a GUI.

SwDevMan81
Wouldnt need InvokeRequired if you use the BackgroundWorker, takes care of that for you
SwDevMan81
Will take a look at it later, thanks.
A: 

Wherever you might be placing your network code, if you update a TextBox many times a second your interface will be unresponsive.

Think about maybe buffering messages in your networking thread instead, and using a timer to update the TextBox only once a second.

Tiberiu Ana
A: 

Also remember to check for InvokeRequired when calling updates to gui controls from other threads.

astander
Thanks, works fine. :)
Had this before aswell, just had to quickly find the property required X-)
astander