Timer and asynchronous

manu aloyau 41 Reputation points
2021-09-29T23:14:43.407+00:00

Hello,
I cannot use the timer with asynchronous.
when i start the timer all the phone is blocked.

I would like to use the msdn tutorial:
https://learn.microsoft.com/fr-fr/dotnet/csharp/programming-guide/concepts/async/
or
https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

But I do not happen I only have text underlined in red.

here is my code (PageAccueil.xaml)

public Info_Boutique infoBoutique = App.InfoBoutique;  
private static  Timer aTimer;  
public void SetTimer()  
{  
    // Créez une minuterie avec un intervalle de deux secondes = 2000.   
    aTimer = new Timer(2000);  
    // Connectez l'événement.    
    aTimer.Elapsed += ModifBandeauDate;  
    aTimer.AutoReset = true;  
    aTimer.Enabled = true;  
    aTimer.Start();  
}  
private void ModifBandeauDate(Object source, ElapsedEventArgs e)  
{  
    NumSemaine.Text = infoBoutique.Semaine_Jeu.ToString();  
    HeureJour.Text = infoBoutique.Heure_Jeu.ToString() + "h";  
    switch (infoBoutique.Jour_Jeu)  
    {  
        case 1:  
            JoursSemaine.Text = "lundi";  
            break;  
  
        case 2:  
            JoursSemaine.Text = "mardi";  
            break;  
. . .   
          
        case 7:  
            JoursSemaine.Text = "dimanche";  
            break;  
    }              
}  
  
  
public AccueilPage()  
{  
    InitializeComponent();  
    infoBoutique.SetTimer();  
   SetTimer();   
}  

here is my code (InfoBoutique.cs)

    public int Heure_Jeu { get; set; } = 8;  
    public int Jour_Jeu { get; set; } = 1;  
    public int Semaine_Jeu { get; set; } = 1;  


    private static Timer aTimer;  

    public void SetTimer()  
    {  
        // Créez une minuterie avec un intervalle de deux secondes = 2000.   
        aTimer = new Timer(2000);  
        // Connectez l'événement Elapsed pour la minuterie.    
        aTimer.Elapsed += OnTimedEvent;  
        aTimer.AutoReset = true;  
        aTimer.Enabled = true;  
        aTimer.Start();  
    }  

    public void OnTimedEvent(Object source, ElapsedEventArgs e)  
    {  
        Heure_Jeu += 1;  
          
        if (Heure_Jeu == 24)  
        {  
            Heure_Jeu = 0;  
            Jour_Jeu += 1;  
        }  

        if (Jour_Jeu == 8)  
        {  
            Jour_Jeu = 1;  
            Semaine_Jeu += 1;  
        }  

    }  

136442-capture-decran-144.png

I can modify the hour + the day by clicking on a button.
But I can't change automatically with the timer, the app freezes.
I think I should use Task or async or await, but I can't use these tools.

the code of the "infoBoutique" class works well.

Hope my translation is ok ;)

Thank you for your help
Manu

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,095 questions
{count} votes

Accepted answer
  1. Viorel 110.7K Reputation points
    2021-10-04T08:18:25.113+00:00

    You should comment the body only, not the whole function.

    It is not clear what Timer class are you using. Check if the next additional instruction helps in your last example:

    private void SetTimer( )
    {
       . . .
       aTimer.SynchronizingObject = this;
    }
    

    If it is not available, then try replacing ‘NumSemaine.Text = x.ToString()’ with this:

    Device.BeginInvokeOnMainThread( ( ) => NumSemaine.Text = x.ToString());
    

    You will also find alternative examples related to Device.StartTimer, which can be used instead of timer classes.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. manu aloyau 41 Reputation points
    2021-10-04T07:02:39.347+00:00

    Hello,
    I work on Visual studio 2019 and with xamarin.

    When I comment out "ModifBandeauDate", the code does not pass, because I use it with "aTimer.Elapsed + = ModifBandeauDate;"

    On the other hand if I do a simple test it also blocks.
    It does not even update the display of the week numbers on the screen.

    137245-capture-decran-145.png

    0 comments No comments

  2. manu aloyau 41 Reputation points
    2021-10-04T08:54:26.16+00:00

    Device.BeginInvokeOnMainThread( ( ) => NumSemaine.Text = x.ToString());

    be my solution, is good.

    Merci
    Manu

    0 comments No comments