user14139029
user14139029

Reputation:

Stop the System.Timers in C#

I'm using System.Timers and the Timer is doing it work perfectly e.g. On Second 4, 8, 12, 16 take a Picture.

But, I want it to Navigate on Second 17, but It won't.

Code

public partial class CustomCameraPage : ContentPage
{
    private static Timer timer_click;
    int Seconds = 0;

    public CustomCameraPage()
    {
        timer_click = new Timer();
        timer_click.Interval = 1000;
        timer_click.Elapsed += OnTimedEvent;
        timer_click.Enabled = true;
        timer_click.AutoReset = true;
        timer_click.Start();

        InitializeComponent();
    }
        
    public void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Seconds++;

        if (Seconds == 4 || Seconds == 8 || Seconds == 12 || Seconds == 16) MessagingCenter.Send<object>(this, "A");
        if (Seconds == 17) Navigation.PushModalAsync(new FormPage());
    }   
}

Upvotes: 0

Views: 86

Answers (1)

Jason
Jason

Reputation: 89082

execute the Navigation on the MainThread

if (Seconds == 17) 
{
  timer_click.Stop();
  MainThread.BeginInvokeOnMainThread(() =>
  {
    Navigation.PushModalAsync(new FormPage());
  });
}

Upvotes: 1

Related Questions