Using 2-4 threads and thread pooling, it is easy to stumble upon some resource conflicts. Remember to
lock
your
StringBuilder
whenever you access it.
Here are some tips if you've never used it:
Remember to distribute the same instance of your lock-token to all threads. The lock-token is just any object, like:
object _myLockToken = new object();
Your code will then be something like:
private void clock_Tick(object sender, EventArgs e)
{
lock(_myLockToken)
{
try
{
txtMsg.Text = myClass.myStringBuilder.ToString();
if (myClass.myStringBuilder.Length == 50000)
{
myClass.myStringBuilder.Clear();
}
}
catch (Exception ex)
{
}
}
}
lock Statement (C# Reference)
I'm quite confident that this will help.
(Remember the BeginInvoke-pattern when updating the GUI)