Show dataset created in own thread in GUI
-
Hi, (.net 4, VS 2010 express) I have a windows form with a DataGridView, a dataset, a timer, and a boolean variable (GUI thread). I have a separate thread where I fill another dataset, created in that thread (data thread), when this thread has filled its own dataset I set the boolean to true. At the timer tick event, in the GUI thread, I check the boolean. If it is true, I copy the data thread's dataset to the GUI thread's dataset (
MyGUIThreadDataSet = MyDataThreadDataSet.Copy
). Then I attach the GUI thread's dataset to the DataGridView (DataGridView.DataSource = MyGUIThreadDataSet
). When I run the application, the lineDataGridView.DataSource = MyGUIThreadDataSet
causes a cross thread error. What am I missing here? Cheers, JohanMy advice is free, and you may get what you paid for.
-
Hi, (.net 4, VS 2010 express) I have a windows form with a DataGridView, a dataset, a timer, and a boolean variable (GUI thread). I have a separate thread where I fill another dataset, created in that thread (data thread), when this thread has filled its own dataset I set the boolean to true. At the timer tick event, in the GUI thread, I check the boolean. If it is true, I copy the data thread's dataset to the GUI thread's dataset (
MyGUIThreadDataSet = MyDataThreadDataSet.Copy
). Then I attach the GUI thread's dataset to the DataGridView (DataGridView.DataSource = MyGUIThreadDataSet
). When I run the application, the lineDataGridView.DataSource = MyGUIThreadDataSet
causes a cross thread error. What am I missing here? Cheers, JohanMy advice is free, and you may get what you paid for.
Hi Johan, did you already try the standard method using a delegate? It's commonly used to avoid cross-thread trouble... This is an untested example - just an idea:
Public Delegate Sub CopyDelegate(ByVal e As CopyEventArgs)
Public Sub UpdateGui(ByVal e As CopyEventArgs)
If InvokeRequired Then
Dim dlg As New CopyDelegate(AddressOf UpdateGui)
BeginInvoke(dlg, New Object() {e.arg1, e.arg2})
Else
' put everything to happen on the GUI here
DataGridView.DataSource = MyGUIThreadDataSet
End If
End SubHope it helps :thumbsup: Michael
modified on Wednesday, May 26, 2010 12:18 PM
-
Hi Johan, did you already try the standard method using a delegate? It's commonly used to avoid cross-thread trouble... This is an untested example - just an idea:
Public Delegate Sub CopyDelegate(ByVal e As CopyEventArgs)
Public Sub UpdateGui(ByVal e As CopyEventArgs)
If InvokeRequired Then
Dim dlg As New CopyDelegate(AddressOf UpdateGui)
BeginInvoke(dlg, New Object() {e.arg1, e.arg2})
Else
' put everything to happen on the GUI here
DataGridView.DataSource = MyGUIThreadDataSet
End If
End SubHope it helps :thumbsup: Michael
modified on Wednesday, May 26, 2010 12:18 PM
This is what is needed. The only thing I would note is that you need to make sure the number of parameters and the type of the parameters match.
BeginInvoke(dlg, New Object() {e.arg1, e.arg2})
won't work because CopyDelegate only takes one parameter and it's the same type as what UpdateGui takes (as it has to be). I believe it should just be
BeginInvoke(dlg, New Object() {e})
Also, you will want to specify which object the InvokeRequired is coming from...as in
If DataGridView1.InvokeRequired Then
...
'And instead of BeginInvoke
DataGridView1.BeginInvoke(...) -
This is what is needed. The only thing I would note is that you need to make sure the number of parameters and the type of the parameters match.
BeginInvoke(dlg, New Object() {e.arg1, e.arg2})
won't work because CopyDelegate only takes one parameter and it's the same type as what UpdateGui takes (as it has to be). I believe it should just be
BeginInvoke(dlg, New Object() {e})
Also, you will want to specify which object the InvokeRequired is coming from...as in
If DataGridView1.InvokeRequired Then
...
'And instead of BeginInvoke
DataGridView1.BeginInvoke(...)Thank you guys both for your help. Cheers, Johan
My advice is free, and you may get what you paid for.