我有線程,它處理一些分析工作。c#線程問題使用從後臺線程調用
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?如果你想在更新網格後執行任何操作,你可以在這裏使用回調函數嗎? – Ram 2010-06-04 04:19:26