Insert data into a real-time text box with Timer [duplicate]

0

I'm using timer as follows:

System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;
aTimer.Enabled = true;

In the OnTimedEvent method I do the following:

var auditoria = auditoriaBll.Retorna_Auditoria();

foreach (var item in auditoria)
{
    //menu e sub menu (categorias)
    if (item.Tabela.Equals("menus")) 
    {
        txtRelatorio.Text = andamento;
    }
}

The following error occurs:

  

Invalid thread operation: control 'txtRelatorio' accessed from a thread that is not the one it was created in.

    
asked by anonymous 20.10.2016 / 13:27

1 answer

1

Basically, you can only access elements created in a thread within this same thread . To resolve, you'll need to use the Invoke

var auditoria = auditoriaBll.Retorna_Auditoria();

foreach (var item in auditoria)
{
    //menu e sub menu (categorias)
    if (item.Tabela.Equals("menus")) 
    {
        this.Invoke(new MethodInvoker(() => txtRelatorio.Text = andamento));
    }
}
    
20.10.2016 / 13:28