我可以在非UI线程中安全地向DataTable添加行吗? [英] Can I safely add rows to a DataTable in non UI thread?

查看:55
本文介绍了我可以在非UI线程中安全地向DataTable添加行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将数据网格的数据源设置为dataTable,是否可以在非UI线程中向该表添加

行而不会导致问题?


或者我真的需要调用在UI线程上运行的方法吗?


注意datagrid是readonly。可以从中选择记录,但不能修改


If I have the datasource of a datagrid set to a dataTable, can I add
rows to that table in a non UI thread w/o causing problems?

Or do I really need to invoke a method running on the UI thread?

Note the datagrid is readonly. Records can be selected from it but not
modified.

推荐答案

wa ******** @ yahoo.com 写道:
如果我有数据网格的数据源设置为dataTable,是否可以在非UI线程中向该表添加
行而不会导致问题?

或者我是否真的需要调用在UI线程上运行的方法?

请注意,datagrid是readonly。记录可以从中选择,但不能修改。
If I have the datasource of a datagrid set to a dataTable, can I add
rows to that table in a non UI thread w/o causing problems?

Or do I really need to invoke a method running on the UI thread?

Note the datagrid is readonly. Records can be selected from it but not
modified.




好​​吧,如果数据网格是只读的,那么用户无法通过其他方式修改它的数据源,它使用单独的线程来更新表格应该是安全的。当两个线程同时访问同一个表时,多线程问题才会发生

,这不是你的情况


BR,

Andrey



Well, if a datagrid is readonly and theer is no other way for a user to modify it''s datasource, it
should be safe to use a separate thread to update the table. The multithreading issues only happen
when two threads access the same table at the same time, which is not your case

BR,
Andrey


MuZZy写道:
wa ******** @ yahoo.com 写道:
wa********@yahoo.com wrote:
如果我将数据网格的数据源设置为dataTable,我可以添加
非UI线程中该表的行没有导致问题?

或者我真的需要调用在UI线程上运行的方法吗?

请注意datagrid是readonly。记录可以从中选择,但不能修改。
If I have the datasource of a datagrid set to a dataTable, can I add
rows to that table in a non UI thread w/o causing problems?

Or do I really need to invoke a method running on the UI thread?

Note the datagrid is readonly. Records can be selected from it but not
modified.



好吧,如果数据网格是只读的,那么用户无法通过其他方式来修改它的数据源,使用单独的线程来更新表应该是安全的。多线程问题只发生在两个线程同时访问同一个表时,这不是你的情况



Well, if a datagrid is readonly and theer is no other way for a user to
modify it''s datasource, it should be safe to use a separate thread to
update the table. The multithreading issues only happen when two threads
access the same table at the same time, which is not your case



我认为DataView(DataData的DefaultDataView) )同时访问这个

表。


Phill,你应该试试这个。我没有尝试过,但也许它会工作。


//从非UI线程调用此代码

Form1Reference.Invoke(new SafeInsertRowDelegate( SafeInsertRow),

new object [] {drow});


// Form1代码

public delegate void SafeInsertRowDelegate(DataRow drow);

private void SafeInsertRow(DataRow drow)

{

DataRow dataRow1 = dataTable1.NewRow;

dataRow1.ItemArray = drow.ItemArray;

dataTable1.Rows.Add(dataRow1);

dataRow1 = null;

}


-

darek



I think that DataView (DefaultDataView of DataGrid) is accessing this
table at the same time.

Phill, You shoud try this. I haven''t tried it, but maybe it will work.

// call this code from non UI thread
Form1Reference.Invoke(new SafeInsertRowDelegate(SafeInsertRow),
new object[] {drow});

// Form1 code
public delegate void SafeInsertRowDelegate(DataRow drow);
private void SafeInsertRow(DataRow drow)
{
DataRow dataRow1 = dataTable1.NewRow;
dataRow1.ItemArray = drow.ItemArray;
dataTable1.Rows.Add(dataRow1);
dataRow1 = null;
}

--
darek


MuZZy< le ******* @ yahoo.com>写道:
MuZZy <le*******@yahoo.com> wrote:
好吧,如果一个数据网格是只读的而且用户没有别的办法来修改它的数据源,那么使用一个单独的线程应该是安全的
更新表格。多线程问题只发生在两个
线程同时访问同一个表时,这不是你的情况
Well, if a datagrid is readonly and theer is no other way for a user
to modify it''s datasource, it should be safe to use a separate thread
to update the table. The multithreading issues only happen when two
threads access the same table at the same time, which is not your
case




不,它'不是并发访问数据表这就是问题

这里 - 事实上,对表的更改将触发UI事件,

将运行在导致更改的线程上。这意味着

不幸的是你只能更改UI线程上的表格。


-

Jon Skeet - < ; sk *** @ pobox.com>
http://www.pobox .com / ~siget

如果回复小组,请不要给我发邮件



No, it''s not concurrent access to the data table which is the problem
here - it''s the fact that changes to the table will trigger UI events,
which will run on the thread which causes the change. That means that
you can only change the table on the UI thread, unfortunately.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


这篇关于我可以在非UI线程中安全地向DataTable添加行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆