Bckground
I have a networked application written in C#. my server program has a UI and several communication threads, that read from tcp sockets and display messages on controller UI.
Communication with each client is done through a seprate thread. When i recieve some stream of messages from one client , the thread for that client writes on UI, which is a richtextbox on a Form.
I call SetTextHelper(string text) method of the form.
which looks like this
private delegate void MyTextUpdateHandler(string text);
public void SetTextHelper(string text)
{
BeginInvoke(new MyTextUpdateHandler(SetText), new object[] { text });
}
public setText(string text)
{
richtext.Text+= text;
}
Question
– If i use BeginInvoke my UI is entirely unresponsive when i m writing large stream of data to UI
– Invoke solves that problem, but i read that for multi threaded environment where many thereads are sharing same resource Invoke can lead to deadlocks I share the common ichtextbox between around 16 threads
– What would be a good desing for my situation?
Invokeshouldn’t lead to deadlocks unless you own a lock while you’re doing the invoking… butBeginInvokeshouldn’t make the UI unresponsive either. Could the problem be that you’re simply trying to update the textbox too often? Perhaps buffer the incoming text more? For example, only update the UI once every half second rather than any time you read any data.