J'ai un thread qui traite un travail analytique.C# Problème de thread à l'aide d'Invoke à partir d'un thread d'arrière-plan
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);
}
}
Je veux mettre à jour en toute sécurité ma GRIDVIEW à boucle, donc j'utilise manière classique:
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;
}
}
Mais quand je rentre UpdateGridSafe, le programme se bloque.
Dans le débogueur, je vois que grid.Invoke n'appelle pas UpdateGridSafe. S'il vous plaît, aidez - quel est le problème?
EDIT
fil classique créer un code
Thread t = new Thread(new ParameterizedThreadStart(ThreadProc));
t.Start(dgvSource);
t.Join();
MessageBox.Show("Done", "Info");
Pourquoi utilisez-vous thread.sleep? Pouvez-vous utiliser la fonction de rappel ici, si vous voulez effectuer une opération après la mise à jour de la grille? – Ram