我有这个脚本:
Action update = () =>
{
dataGridMaterials.DataSource = null;
dataGridMaterials.AutoGenerateColumns = false;
dataGridMaterials.DataSource = materials;
dataGridMaterials.Refresh();
};
var invoke = dataGridMaterials.BeginInvoke(update);
if (invoke != null) dataGridMaterials.EndInvoke(invoke);材料有很多元素。它应该重新加载DataGridView,但它没有。我只知道BeginInvoke没有调用该操作。
有没有其他更新DataGridView的方法?(.NET框架4)
发布于 2016-12-23 23:48:53
BeginInvoke在“UI线程”上发布了一条消息。一旦该线程空闲,它就会拾取消息并处理它。
如果您共享的代码在UI线程上运行,那么您最好直接执行操作,而不是使用BeginInvoke。
如果代码不在UI线程上运行,那么我能想到的不运行操作的唯一原因是UI线程正在等待这段代码完成,即:
void MyMethodCalledOnUIThread()
{
Action update = () =>
{
...
};
ManualResetEvent mre = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback((o) =>
{
dataGridMaterials.EndInvoke(dataGridMaterials.BeginInvoke(update));
mre.Set();
}), null);
mre.WaitOne();
}这将导致UI线程和ThreadPool线程相互等待,并且整个UI将停止响应。
https://stackoverflow.com/questions/41304076
复制相似问题