question

manualoyau-4681 avatar image
0 Votes"
manualoyau-4681 asked Viorel-1 answered

Timer and asynchronous

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://docs.microsoft.com/fr-fr/dotnet/csharp/programming-guide/concepts/async/
or
https://docs.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

dotnet-csharp
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


Does it block if you temporarily comment the body of ModifBandeauDate?

What kind of application is this? (A Xamarin one?)



0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

manualoyau-4681 avatar image
0 Votes"
manualoyau-4681 answered Viorel-1 converted comment to answer

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



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

manualoyau-4681 avatar image
0 Votes"
manualoyau-4681 answered

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

be my solution, is good.

Merci
Manu

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.