J'utilise Devexpress GridControl. Et je veux changer la valeur de la cellule en fonction des messages tcp.C# System.Reflection.TargetInvocationException avec multithread
private void Form1_Load(object sender, EventArgs e)
{
grid_mygrid.DataSource = Get_Devices_Info.Get_Device_From_DB().Tables[0];
this.tcpListener = new TcpListener(IPAddress.Any, 2000);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Priority = ThreadPriority.Highest;
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
TcpClient client = this.tcpListener.AcceptTcpClient();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
...
try
{
gridView1.SetRowCellValue(row_index, "boot_progress", 50);
}
catch(Exception e)
{
if (e.InnerException != null)
{
Log2LogText(e.InnerException.Message);
}
}
...
}
si je veux essayer ceci sous le fil
gridView1.SetRowCellValue(row_index, "boot_progress", 50);
si ROW_INDEX = 0 -> Il est OK
si ROW_INDEX> 0 ->Une exception non gérée du type « System.Reflection .TargetInvocationException » est produite dans mscorlib.dll
A propos GridControl si j'essayer avec cliquez sur le bouton
gridView1.SetRowCellValue(1, "boot_progress", 50); -> It is OK
gridView1.SetRowCellValue(2, "boot_progress", 50); -> It is OK
gridView1.SetRowCellValue(4, "boot_progress", 50); -> There is no row with index 4. But there is no error. It is OK.
gridView1.SetRowCellValue(5, "random_column", 50); -> There is no column with "random_column". But there is no error. It is OK.
J'utilise try-catch mais ce code donne toujours une erreur et un arrêt du programme. Quand je commente cette ligne
gridView1.SetRowCellValue(row_index, "boot_progress", 50);
il n'y a pas de problème
Je suis en attente de vos suggestions.
Lisez l'InnerException. – SLaks
L'interface utilisateur n'est pas adaptée aux threads. Vous devez exécuter sur le thread UI. – SLaks
Vous devez séparer votre code d'interface utilisateur de votre TCP/logic. – SLaks