I'm quite new to Asynchronous and I understand some general concepts, but I cannot seem to fix couple of issues.
I have the following ReceiveCallback:
public static void ReceiveCallback(IAsyncResult AR)
{
Socket CurrentSocket = (Socket)AR.AsyncState;
int DataReceived = 0;
try
{
DataReceived = CurrentSocket.EndReceive(AR);
}
catch (SocketException)
{
CurrentSocket.Close();
return;
}
byte[] receivedBuffer = new byte[DataReceived];
Array.Copy(Buffer, receivedBuffer, DataReceived);
strReceived = Encoding.ASCII.GetString(receivedBuffer); // We are saving the latest receivedBuffer in a string.
new MainWindow().Process(); // We are accessing a function in the MainWindow class
receiveDone.Set();
CurrentSocket.BeginReceive(Buffer, 0, BufferSize, SocketFlags.None, ReceiveCallback, CurrentSocket);
}
Here's the "Process()" code:
public void Process()
{
lblReceived.Text = ClientSocket.strReceived; // Trying to set what we received in a label..
}
On the line: "new MainWindow().Process();", I receive the following exception: "Additional information: The calling thread must be STA, because many UI components require this." I've searched, and understood why it gives me the exception but several code did not work for me.
The label doesn't change, I know that I am creating a new instance of MainWindow and that's why nothing shows, but I'm seeking for example on how I can make the label change by using a new instance.