How to use the async function of System.timer.Timers?

Asked

Viewed 28 times

0

I’m trying to deploy an asynchronous timer in my application. NET, so that the Forms is interactive while the back end makes a request and deserializes the answer of it, assembling everything in a virtual table that will update the Forms. When I tried to deploy the example available in Microsoft Docs I did not get results, it only got worse when I decided to use System.timer.Timer.Synchronizingobject = this to use with the Forms in question:

private System.Timers.Timer tempo = new System.Timers.Timer(10000);
        
    try
            {
                tempo.SynchronizingObject = null;
                tempo.AutoReset = true;// no exemplo não usava  mas tentei colocar pra ver se haveria alguma mudança
                tempo.Elapsed += async (sender, e) => await UpdateTimerCTRL_Tick();
               // ThreadPool.QueueUserWorkItem(UpdateTimerCTRL_Tick);
                tempo.Start();
            }
            catch (Exception exception)
            {
                ClientUtils.HandleException(this.Text, exception);
                tempo?.Dispose();
            }
            //SiemensOPC2OPCUA();
        }
        private async Task UpdateTimerCTRL_Tick()
        {
            try
            {
                ////////////////////////
                CadeiaAtualiza();///faz a atualização da tabela virtual
                ////////////////////////
            }
            catch (Exception exception)
            {
                ClientUtils.HandleException(this.Text, exception);

            }
        }

the main error is even making the table mount not update and not having the thread end, in the long run happens a thread looting error.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.