I'm working on a C# WinForms
project.
I try to work with multi threading
in a part of my App :
Part code (Method) :
public void ShowData()
{
Thread t;
t = new Thread(() =>
{
using (GVentesEntities context = new GVentesEntities())
{
dataGridView1.DataSource = (from F in context.Facture.AsEnumerable() join C in context.Client.AsEnumerable() on F.CLT equals C.ID select new { ID = F.ID, F.Date, Client = $"{C.Nom} {C.Prenom}", Total = $"{(from df in context.Det_Fact.AsEnumerable() join cmd in context.Commande.AsEnumerable() on df.CMD equals cmd.ID where df.FACT == F.ID select cmd.QTE * cmd.PRU).Sum():00} MAD" }).OrderBy(x => int.Parse($"{x.ID.Substring(x.ID.IndexOf("-") + 1)}")).ToList();
}
});
Thread.Sleep(1000);
t.Start();
}
The problem is when I try to access this method when application is run I get an error :
Error
System.InvalidOperationException: 'Cross-thread operation not valid: Control 'dataGridView1' accessed from a thread other than the thread it was created on.
'
Please any help to handle this problem and execute my method with threading ? thanks in advance.