如何解決「Control.Invoke必須用於與一個獨立的線程創建控件交互」錯誤異常如何解決Winforms 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();
編輯代碼片段以顯示失敗的代碼。 – 2010-06-13 09:39:07
你肯定確定它是導致異常的這一點代碼嗎?實際上(隱式地)創建UI控件的唯一地方是通過調用MessageBox.Show,但我迄今認爲這種方法不必在UI(「main」)線程上運行。你真的在程序中使用多線程嗎? – stakx 2010-06-13 10:13:32