[問題] thread的使用???

看板C_Sharp作者 (...)時間16年前 (2009/10/12 17:49), 編輯推噓1(1011)
留言12則, 5人參與, 最新討論串1/1
想請問一下 以下程式碼thread的使用是否有誤??? 因為我使用button做Recieve的動作時 在richTextBox1會新增文字 但是使用thread後 卻無法顯示... 感覺起來它是有接收到別人傳來的訊息 但是卻顯示不出來... 謝謝 -- private void Form1_Load(object sender, EventArgs e) { //IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0]; IPAddress[] ips; string hostname = "****"; ips = Dns.GetHostAddresses(hostname); myTcpListener = new TcpListener(ips[0], 36000); myTcpListener.Start(); mySocket = myTcpListener.AcceptSocket(); try { if (mySocket.Connected) { label1.Text = "連線中"; bb = new NetworkStream(mySocket); //timer1.Start(); } } catch (Exception ee) { label1.Text = ee.Message; } Thread T = new Thread(new ThreadStart(myRecieve)); T.Start(); } private void myRecieve() { while(true) { try { int dataLength; byte[] myBufferBytes = new byte[1000]; if (mySocket.Connected) { dataLength = mySocket.Receive(myBufferBytes, 1000, 0); richTextBox1.Text += "Client: " + Encoding.Unicode. GetString(myBufferBytes, 0, dataLength); richTextBox1.Text += "\n\r"; } } catch (Exception ee) { } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.18.218

10/12 21:17, , 1F
除了建立UI Control的那條執行緒外,不可以其它的去存取
10/12 21:17, 1F

10/12 21:18, , 2F
看起來 Windows Form, check InvokeRequired property
10/12 21:18, 2F

10/12 21:19, , 3F
然後看看有沒有 Invoke() function 之類的
10/12 21:19, 3F

10/12 21:22, , 4F
有一篇文章有講到這個:http://tiny.cc/1za7h
10/12 21:22, 4F

10/12 23:27, , 5F
想請問一下 thread跟involk的關係是??? @ @"
10/12 23:27, 5F

10/13 03:17, , 6F
還有請問一下 UI control是指form1的那個介面嗎???
10/13 03:17, 6F

10/14 09:14, , 7F
這我常用到 請參考1F提到的網址 要多設一個delegate
10/14 09:14, 7F

10/14 09:14, , 8F
然後你自己新增的thread要透過該delegate去存取
10/14 09:14, 8F

10/14 09:15, , 9F
UI上的Component 如TextBox之類
10/14 09:15, 9F

10/15 14:19, , 10F
嗯嗯 已經試成功了 謝謝~
10/15 14:19, 10F

10/16 20:37, , 11F
M$ VS 應該會跳出視窗,說你不可以直接存取才對?
10/16 20:37, 11F

10/16 23:04, , 12F
10/16 23:04, 12F
文章代碼(AID): #1AqlkKDX (C_Sharp)