[問題] 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 17:58, , 1F
10/12 17:58, 1F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章