【发布时间】:2011-05-22 22:39:45
【问题描述】:
我是 C# 编程的新手,我正在开发一个基于 TcpClient 的应用程序。
我想知道如何使用 BeginRead 和 EndRead,我已经阅读了 MSN 文档但没有帮助。
我有这个:
private void Send() { TcpClient _client = new TcpClient("host", 80); NetworkStream ns = _client.GetStream(); ns.Flush(); / ... ns.Write(buffer, 0, buffer.Length); int BUFFER_SIZE = 1024; byte[] received = new byte[BUFFER_SIZE]; ns.BeginRead(received, 0, 0, new AsyncCallback(OnBeginRead), ns); } private void OnBeginRead(IAsyncResult ar) { NetworkStream ns = (NetworkStream)ar.AsyncState; int BUFFER_SIZE = 1024; byte[] received = new byte[BUFFER_SIZE]; string result = String.Empty; ns.EndRead(ar); int read; while (ns.DataAvailable) { read = ns.Read(received, 0, BUFFER_SIZE); result += Encoding.ASCII.GetString(received); received = new byte[BUFFER_SIZE]; } result = result.Trim(new char[] { '\0' }); // Want to update Form here with result }
如何使用结果更新表单组件?
感谢您的帮助。
【问题讨论】:
-
我在这里遇到了同样的问题,我想知道是否可以在您插入评论行以更新表单的位置发起事件。从Form 订阅事件并更新什么?还是在单独的线程中会导致一些问题?谢谢。