How to timer page display

kztnedakztneda Member ✭✭

Hello,
I have different pages on my proyect but I need to display each one in an especific time and then hide the page to display another.
For that I guess I have to use a Timer using "Device.StartTimer" but if I try to invoke any page inside of it, the page doesn't display. And if I use a simple loop with "Thread.Sleep" obiously the app gets locked the time I set and the user can't use the app while it, but the pages are displayed correctly.
how can solve this problem?

Best Answer

  • kztnedakztneda ✭✭
    Accepted Answer

    at the end I solved it using a "while(BOOL_VAR)" and Task.Delay(x)

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @kztneda - Post your code

  • kztnedakztneda Member ✭✭
    edited July 2018

    @JohnHardman here it is

    Device.StartTimer(TimeSpan.FromSeconds(TIME), ()=> 
    {
        try
        {
             Navigation.PopModalAsync(true);//It doesn't accept "await" inside the timer
        }
        catch (Exception e) { }
       //TODO
       Navigation.PushModalAsync(PAGE); //It doesn't accept "await" inside the timer
    
       return false;
    });
    
  • JohnHardmanJohnHardman GBUniversity mod
    edited July 2018

    @kztneda said:
    Device.StartTimer(TimeSpan.FromSeconds(TIME), ()=>
    {

       try
       {
              Navigation.PopModalAsync(true);
        }
        catch (Exception e) { }
       //TODO
       Navigation.PushModalAsync(PAGE); //It doesn't accept "await" inside the timer
    
    
       return false;
    });
    

    Putting async before the ()=> will allow you to await the PushModalAsync and PopModalAsync.

    Note that you'll want to use Device.BeginInvokeOnMainThread as well.

  • kztnedakztneda Member ✭✭

    @JohnHardman said:

    @kztneda said:
    Device.StartTimer(TimeSpan.FromSeconds(TIME), ()=>
    {

       try
       {
              Navigation.PopModalAsync(true);
        }
        catch (Exception e) { }
       //TODO
       Navigation.PushModalAsync(PAGE); //It doesn't accept "await" inside the timer
    
    
       return false;
    });
    

    Putting async before the ()=> will allow you to await the PushModalAsync and PopModalAsync.

    Note that you'll want to use Device.BeginInvokeOnMainThread as well.

    I tried like this:

    Device.StartTimer(TimeSpan.FromSeconds(TIME), () => 
    {
         Device.BeginInvokeOnMainThread(() =>//with and without async in here, but none works
         {
              try
              {
                   Navigation.PopModalAsync(true);
               }
               catch (Exception e) { }
               //TODO
               Navigation.PushModalAsync(PAGE);
      }
     }
    
  • JohnHardmanJohnHardman GBUniversity mod

    @kztneda said:
    I tried like this:

    Device.StartTimer(TimeSpan.FromSeconds(TIME), () => 
    {
         Device.BeginInvokeOnMainThread(() =>//with and without async in here, but none works
         {
              try
              {
                   Navigation.PopModalAsync(true);
               }
               catch (Exception e) { }
             //TODO
               Navigation.PushModalAsync(PAGE);
    }
     }
    

    When you say "but none works", are you saying that you cannot get it to compile, or that it builds but does not do what you want when it runs? I am trying to understand which you need assistance with.

    If you need help to get it to compile, go back to the code you had previously, add the two awaits, and add the async before () => . Only once that is compiling then put in the Device.BeginInvokeOnMainThread, move the async, and do not remove the return false; . Make sure that you understand why each change is necessary before progressing.

    Once those changes are made and compiling, the next question is does it do what you want it to do?

  • kztnedakztneda Member ✭✭
    Accepted Answer

    at the end I solved it using a "while(BOOL_VAR)" and Task.Delay(x)

Sign In or Register to comment.