I am working in WPF and I have a dialog window that start a listen socket, and is supposed to close as soon as someone connect. Here is my naive, non working snippet code:
void acceptCallback(IAsyncResult iar)
{
socket = listenSocket.EndAccept(iar);
DialogResult = true; // error here
Close();
}
private void ValidButton_Click(object sender, RoutedEventArgs e)
{
IPEndPoint iep = new IPEndPoint(IPAddress.Any, port);
listenSocket.Bind(iep);
listenSocket.Listen(1);
listenSocket.BeginAccept(acceptCallback, null);
}
I get an error telling me that DialogResult can't be accessed from this thread, I understand that my "acceptCallback" function is called from the thread running the accept asynchronously, but don't really know how to get the behavior I want.
How can I tell the main thread from this callback that it is supposed to close the dialog window in a proper way ?