如何解决“ Control.Invoke必须用于与在分离的线程上创建的控件进行交互”错误异常
Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(IPAddress.Loopback, portNo));
serverSocket.Listen(5);
Socket client = serverSocket.Accept();
MessageBox.Show("Client Connected");
//Sent to Client
NetworkStream ns = new NetworkStream(client);
StreamWriter writer = new StreamWriter(ns);
writer.AutoFlush = true;
writer.WriteLine(sb.ToString());
//Receive from Client NetworkStream
Stream nets = new NetworkStream(client);
StreamReader reader = new StreamReader(nets);
string clientIPAddress = reader.ReadLine();
答案 0 :(得分:0)
通过做它说的话?这听起来像是在进行某种回调,但是(由于线程亲和性),您只能与其关联线程上的控件进行对话。所以:
string newAnswer = LongComplexCode(); // on worker thread here
someControl.Invoke((MethodInvoker) delegate {
/* your work here, e.g. */
someControl.Text = newAnswer; // on UI thread here
});