This is the error :
**
Exception thrown: 'System.InvalidOperationException' in System.Windows.Forms.dll Additional information: Cross-thread operation not valid: Control 'displayText' accessed from a thread other than the thread it was created on.
**
I was created multi threaded client and server application using on C#. I was researched about this error but I could not find any relevant answer. I know this is comes when two or more threads started on the program...But my server side has one thread...I don't know why this is comes..........
Here is my Server side:
private void Handler()
{
try {
byte[] b = new byte[100];
int k = s.Read(b, 0, b.Length);
//int k = s.Receive(b);
string szReceived = Encoding.ASCII.GetString(b,0,k);
//If the data to be converted is available only in sequential blocks (such as data read from a stream) or if the amount of data is so large that it needs to be divided into smaller blocks,
while (ServerRunning)
{
string ConcatString = "";
for (int i = 0; i < k; i++)
{
char n = Convert.ToChar(b[i]);
string chars = Convert.ToString(n);
ConcatString = ConcatString + chars;
}
if (b[0] == '$')
{
displayText.AppendText("\nPrivate Message");
//MessageBox.Show("\nPrivate Message" + Environment.NewLine);
}
else
{
displayText.AppendText("\n" + ConcatString);
//MessageBox.Show(ConcatString + Environment.NewLine);
}
//Encoding is the process of transforming a set of Unicode characters into a sequence of bytes and using new instance
ASCIIEncoding asen = new ASCIIEncoding();
//s.Send(asen.GetBytes("The string was recieved by the server." + Environment.NewLine));
displayText.AppendText("\n" + ConcatString);
/* clean up */
//*
// k = s.Receive(b);
s.Close();
client.Close();
//MessageBox.Show("Recieved..." + Environment.NewLine);
}
}
catch(Exception ex)
{
MessageBox.Show("Error ...." + ex);
}
}
I am new to Socket Programming, but I was researched each and every code segment and experimented the code in several times.. Still can't figure out what exactly I missed in this program...
So please help me to solve this...I will be appreciated very much... Thanks..