110,566
社区成员
发帖
与我相关
我的任务
分享
READ_A.netStream.BeginWrite( READ_A.writeBytes,
0,
READ_A.writeBytes.Length,
new AsyncCallback(SendCallback),
READ_A );
private void SendCallback(IAsyncResult ar)
{
ReadWriteObject READ_A = (ReadWriteObject)ar.AsyncState;
。。。。。
}
void Test()
{
Action action = delegate { Thread.Sleep(1000); };
action.BeginInvoke(ActionEnded, "额外数据");
action.BeginInvoke(ActionEnded, 1234);
}
void ActionEnded(IAsyncResult result)
{
if (result.AsyncState is string) MessageBox.Show("用户数据是字符串:" + (string)result.AsyncState); // 额外数据
if (result.AsyncState is int) MessageBox.Show("用户数据是整数:" + (int)result.AsyncState); // 1234
//...
}
class ReadWriteObject
{
public TcpClient client;
public NetworkStream netStream;
public byte[] readBytes;
public byte[] writeBytes;
public ReadWriteObject(TcpClient client)
{
this.client = client;
netStream = client.GetStream();
readBytes = new byte[client.Size1];
writeBytes = new byte[client.Size2];
}
.........
}