TCP 客户端填充填充绑定 DataGridView 的 DataTable,UI 无响应
首先,我使用 Visual Studio 10 并编写 Windows 窗体应用程序。我对 C# 中的线程没有经验。
我有一个 C# 应用程序,它使用我的 C# DLL 来侦听网络流,并解析它接收到的数据。解析的数据用于插入/更新数据表的行,该数据表绑定到位于主窗体上的 DataGridView。
我首先尝试使用在 DLL 内部启动的工作线程。绑定到 DataGridView 的 DataTable 作为参数传递给 DLL。然后线程开始。线程函数是这样的;
private void ListenThread()
{
Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.InvariantCulture;
while (m_Active)
{
Thread.Sleep(100);
int lData = m_Stream.Read(m_Buffer, 0,
m_Client.ReceiveBufferSize);
String myString = Encoding.UTF7.GetString(m_Buffer);
myString = myString.Substring(0, lData);
ParseString(myString);
}
}
ParseString() 方法解析数据并向 DataTable 中插入一行或更新现有行。
这段代码运行良好,直到我尝试使用 CTRL+F5 而不是 F5 来运行该应用程序。几秒钟后,UI 开始填满网格,然后变得没有响应。
我用 google 搜索了一下,发现我应该使用 BeginInvoke 来防止 UI 冻结。但我没有成功地实现这一点。
我尝试了类似的方法
TCPListener Listener = new TCPListener(ListenThread);
IAsyncResult result = Listener.BeginInvoke(null, null);
,
Thread m_tidListen = new Thread(new ThreadStart(ListenThread));
但效果相同。仍然无法使用“无调试模式”。
我应该如何使用 BeginInvoke 来实现它?或者我应该尝试别的东西?
First of all, I am using Visual Studio 10 and coding Windows Forms App. I am not experienced with threads in C#.
I have a C# app which uses my C# DLL that listens to a Network Stream, and parses the data it receives. The parsed data is used to insert/update the rows of the Datatable which is bound to the DataGridView that is located on the main form.
I have tried this first with a worker thread which is started inside the DLL. The DataTable which is bound to the DataGridView is passed as a parameter to the DLL. And then the thread starts. The thread function was something like this;
private void ListenThread()
{
Thread.CurrentThread.CurrentCulture =
System.Globalization.CultureInfo.InvariantCulture;
while (m_Active)
{
Thread.Sleep(100);
int lData = m_Stream.Read(m_Buffer, 0,
m_Client.ReceiveBufferSize);
String myString = Encoding.UTF7.GetString(m_Buffer);
myString = myString.Substring(0, lData);
ParseString(myString);
}
}
The ParseString() method parses the data and inserts a row to the DataTable or updates the existing ones.
This code was working well, until I tried to run the app with CTRL+F5 instead of F5. The UI became unresponsive after a few seconds later it began to fill the Grid.
I have googled this and found that I should use BeginInvoke to prevent the UI from freezing. But I was not successful to implement that.
I tried something like
TCPListener Listener = new TCPListener(ListenThread);
IAsyncResult result = Listener.BeginInvoke(null, null);
instead of
Thread m_tidListen = new Thread(new ThreadStart(ListenThread));
but it worked the same way. Still doesnt work with "without debugging mode".
How should I implement this with BeginInvoke? Or should I try something else?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您已经在构建 winform,因此您不妨使用 BackGroundWorker< /a> 对于这种事情。它们是为在后台执行操作而设计的,同时保持表单的响应能力。例如:
Since you are building winforms already, you might as well use a BackGroundWorker for this kind of thing. They are made for doing stuff in the Background while keeping your form responsive. For example: