我试图访问messageRecieved
的字符串,该字符串应传递给frmMain.Sort_Data(messageRecived);
。但是一旦它到达这里就null
。 Console.WriteLine
显示正确的数据。
public class Client
{
frm_main frmMain = new frm_main();
public string ReturnDat;
//for TCP communications
TcpClient _client = null;
//for sending/receiving data
byte[] data;
public Client(TcpClient client)
{
_client = client;
//start reading data from the client in a separate thread
data = new byte[_client.ReceiveBufferSize];
_client.GetStream().BeginRead(
data, 0, _client.ReceiveBufferSize, receiveMessage, null);
}
public void receiveMessage(IAsyncResult ar)
{
//read from client
int bytesRead;
lock (_client.GetStream())
{
bytesRead = _client.GetStream().EndRead(ar);
}
//if client has disconnected
if (bytesRead < 1)
return;
else
{
//get the message sent
string messageReceived =
ASCIIEncoding.ASCII.GetString(data, 0, bytesRead);
//Console.WriteLine(messageReceived);
frmMain.Sort_Data(messageReceived);
//ReturnDat = messageReceived;
}
//continue reading from client
lock (_client.GetStream())
{
_client.GetStream().BeginRead(
data, 0, _client.ReceiveBufferSize,
receiveMessage, null);
}
}
}
下面是Sort_Data
,它在课程中点击时不返回任何内容。
Public void Sort_Data(string data)
{
Messagebox.show(data);
}
也许我错过了什么,或者没有正确地说些什么?