[問題] thread的使用???
想請問一下
以下程式碼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
10/12 21:17, 1F
→
10/12 21:18, , 2F
10/12 21:18, 2F
→
10/12 21:19, , 3F
10/12 21:19, 3F
→
10/12 21:22, , 4F
10/12 21:22, 4F
→
10/12 23:27, , 5F
10/12 23:27, 5F
→
10/13 03:17, , 6F
10/13 03:17, 6F
→
10/14 09:14, , 7F
10/14 09:14, 7F
→
10/14 09:14, , 8F
10/14 09:14, 8F
→
10/14 09:15, , 9F
10/14 09:15, 9F
→
10/15 14:19, , 10F
10/15 14:19, 10F
推
10/16 20:37, , 11F
10/16 20:37, 11F
→
10/16 23:04, , 12F
10/16 23:04, 12F