Invoke vs .... (for thread communication with UI controls)

E

_e_

The common approach to handling thread-to-UI sync seems to be Invoke
(as per Chris Sells "PI generator" etc).

Just wondering if there is any problem with having the thread write
data to shared variables and using a timer on the UI thread to check
for changes periodically.

I need to collect data a fill DataTables in the background, while
updating a DataGrid control. Since I have to collect and store the
data anyway, it seems like it would be just as easy to set up a timer
in the main (UI) thread.

Here's another issue: Are any problems encountered if a DataGrid
control is bound to a DataTable, and the background thread updates the
DataTable?
 
N

Nicholas Paldino [.NET/C# MVP]

You could do that, but you have to make sure to wrap the access to the
shared variable in a lock statement, so that one thread is not updating it
while another is reading it.

However, I wouldn't recommend this approach. It's more code you have to
write for a solution that is already in place which addresses your need.

As for updating a data table which a grid is bound to in a thread other
than the one that created a data table, there is a problem with doing that.
When the data table fires the events that the grid binds to, those events
come in on the thread that performed the update. If you want to update a
datatable bound to a source, you should perform the updates on the UI
thread.

Hope this helps.
 
E

_e_

You could do that, but you have to make sure to wrap the access to the
shared variable in a lock statement, so that one thread is not updating it
while another is reading it.

However, I wouldn't recommend this approach. It's more code you have to
write for a solution that is already in place which addresses your need.

The 'invoke' mechanism? Not sure that solves this one. In this case,
the main intent is getting data. The display (via DataGrid) is a
secondary concern, and could be done in less time-critical fashion.

See below...
As for updating a data table which a grid is bound to in a thread other
than the one that created a data table, there is a problem with doing that.
When the data table fires the events that the grid binds to, those events
come in on the thread that performed the update. If you want to update a
datatable bound to a source, you should perform the updates on the UI
thread.

I appreciate your insight. I was afraid you were going to say that.
What is the best way to marshal the data then? Controls may have the
'invoke' functions, but the equivalent needs to be done for the
DataTable.

You'd think this would be a commonly encountered problem, but I have
not found any references to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top