private void MessageCallBack(IAsyncResult aResult)
{
int size = sck.EndReceiveFrom(aResult, ref epRemote);
if (size > 0)
{
byte[] recevivedData = new byte[1464];
recevivedData = (byte[])aResult.AsyncState;
ASCIIEncoding eEncoding = new ASCIIEncoding();
string recevieMessage = eEncoding.GetString(recevivedData);
listBox1.Items.Add("Friend:" + recevieMessage); //In this Line Im getting error Cross-thread operaation not valid: Control 'listBox1' accessed from a thread other than the thread it was created on
i can send msg to my friend but while receiving the message im getting the error as i mentioned above so pls help me..
}
byte[] buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
akshay rasalakarPosted Jun 15, 2015, 12:08 PM
Manoj BhoirPosted Jun 14, 2015, 12:19 AM
You can use Control.InvokeRequired[^] to determine if you're on the UI tread or not. If you're not on the UI thread you have to use Control.Invoke[^] or Control.BeginInvoke[^]
{
listBox1.Items.Add("Friend:" + recevieMessage);
}));