【发布时间】:2010-06-04 03:48:10
【问题描述】:
我有线程,它处理一些分析工作。
private static void ThreadProc(object obj)
{
var grid = (DataGridView)obj;
foreach (DataGridViewRow row in grid.Rows)
{
if (Parser.GetPreparationByClientNameForSynonims(row.Cells["Prep"].Value.ToString()) != null)
UpdateGridSafe(grid,row.Index,1);
Thread.Sleep(10);
}
}
我想在循环中安全地更新我的 gridView,所以我使用经典方式:
private delegate void UpdateGridDelegate(DataGridView grid, int rowIdx, int type);
public static void UpdateGridSafe(DataGridView grid, int rowIdx, int type)
{
if (grid.InvokeRequired)
{
grid.Invoke(new UpdateGridDelegate(UpdateGridSafe), new object[] { grid, rowIdx, type });
}
else
{
if (type == 1)
grid.Rows[rowIdx].Cells["Prep"].Style.ForeColor = Color.Red;
if (type==2)
grid.Rows[rowIdx].Cells["Prep"].Style.ForeColor = Color.ForestGreen;
}
}
但是当我进入 UpdateGridSafe 时,程序就挂了。
在调试器中,我看到 grid.Invoke 没有调用 UpdateGridSafe。请帮忙 - 怎么了?
编辑
经典线程创建代码
Thread t = new Thread(new ParameterizedThreadStart(ThreadProc));
t.Start(dgvSource);
t.Join();
MessageBox.Show("Done", "Info");
【问题讨论】:
-
你为什么使用thread.sleep?如果要在更新网格后执行任何操作,可以在这里使用回调函数吗?
标签: c# .net multithreading