BindingSource.ResetCurrentItem一次仅适用于1个控件 [英] BindingSource.ResetCurrentItem only works for 1 control at a time
问题描述
也许有人可以帮助我.
(使用VB.NET 2010)
我有一个简单的vb.net应用程序,用于测试如何通过更改数据集中的数据来从其他线程更改控件的属性.
我设置了以下内容:
1个具有1个表和1行的数据集
1个表格,其中2个文本框绑定到表中的同一行.
在表单内部,我启动了一个更改行值的线程.
我在表单内使用计时器,使用BindingSource.ResetCurrentItem()
更新文本框.
结果是随机更新了第一个或第二个文本框,但从未同时更新.
我怎么了?
这是代码:
Hi,
perhaps someone can help me.
(using VB.NET 2010)
I have a simple vb.net app to test how to change properties of a control from a different thread by changing data in a dataset.
I set up the following:
1 Dataset with 1 table and 1 row
1 Form with 2 Textboxes bound to the same row in the table.
Inside the form I start a thread changing the value of the row.
I use a timer inside the form to update the Textboxes with BindingSource.ResetCurrentItem()
The result is that randomly the first or the second textbox is updated, but never both on the same time.
What did I wrong?
here is the code:
Imports System.Threading
Public Class Form1
Private t As System.Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
table.Rows.Add(row)
t = New Thread(AddressOf mDataReader)
t.Priority = ThreadPriority.Lowest
t.Start()
End Sub
Public Shared Sub mDataReader()
MsgBox("Tread Start")
Do While Dothread = True
row("C1") = Now.Ticks.ToString
Loop
End Sub
Protected Overrides Sub OnFormClosed(ByVal e As System.Windows.Forms.FormClosedEventArgs)
MsgBox("FINISH !!")
Dothread = False
Try
t.Join()
Catch
End Try
MyBase.OnFormClosed(e)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Table1BindingSource.ResetCurrentItem()
End Sub
End Class
Module Module1
Public Dothread As Boolean = True
Public table As DataTable = Form1.DataSet1.Tables("table1")
Public row As DataRow = table.NewRow()
End Module
亨利,
是的,你是完全正确的.问题是从另一个线程访问数据.
那么,什么是解决我问题的正确方法?
我有一个正在与速度较慢的设备通信的线程. (我不想挂起UI)以后,用户可以添加控件(也许几百个),这些控件应该使用来自该线程的数据进行更新.因为无法同时从其他线程更改属性,您是否知道如何管理它?
Henry,
yes, you are completely right. The problem is accessing the data from a different thread.
So, what would be the right way for my problem?
I have a Thread which is communicating with a slow device. (I don’t want to hang the UI) The User later can add controls (perhaps hundreds) which should be updated with the data coming from this thread. Any idea how to manage that, because properties can''t be changed from a different Thread as well ?
推荐答案
尝试BindingSource.ResetBindings()
.
这篇关于BindingSource.ResetCurrentItem一次仅适用于1个控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!