private void MessageCallBack(IAsyncResult aResult)
{
try
{
byte[] receivedData = new byte[1500];
receivedData = (byte[])aResult.AsyncState;
//Converting byte[] to string
ASCIIEncoding aEncoding = new ASCIIEncoding();
string receivedMessage = aEncoding.GetString(receivedData);
//Adding this message into Listbox
listMessage.Items.Add("Friend : " + receivedMessage);
//TextBox.CheckForIllegalCrossThreadCalls = false;
buffer = new byte[1500];
sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Asked
Active
Viewed 29 times
0

musefan
- 47,875
- 21
- 135
- 185
-
I am using this code in my chat application., and this error occurs ,i dont know how to proceed – Vishwanath Sharma Mar 13 '18 at 16:28
-
1first dont put the question in a comment. THats what the question is for. Put the question in the question. – pm100 Mar 13 '18 at 16:30
-
second - say what platform . Is this winforms xamarin WPF ..... – pm100 Mar 13 '18 at 16:30
-
third say where the error is happening – pm100 Mar 13 '18 at 16:30
-
ok. Can you help me? – Vishwanath Sharma Mar 13 '18 at 16:31
-
I am using Visual Studio 2017 . I am using 2 laptops to test the chat application when i try to send message to other laptop using this code , this error occurs – Vishwanath Sharma Mar 13 '18 at 16:32
-
listMessage.Items.Add("Friend : " + receivedMessage); – Vishwanath Sharma Mar 13 '18 at 16:33
-
is this winforms? – pm100 Mar 13 '18 at 16:33
-
Possible duplicate of [Cross-thread Winforms control editing](https://stackoverflow.com/questions/2562946/cross-thread-winforms-control-editing) – pm100 Mar 13 '18 at 16:34
-
how do i edit my code – Vishwanath Sharma Mar 13 '18 at 16:37
-
did you read the duplicates? I am not going to repeat what they say, try googling 'winforms cross thread invoke' for more info – pm100 Mar 13 '18 at 16:39