Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Is there a better way to do this? (Async/Await Timer)

$
0
0

I'm building an app based around a simple stopwatch feature. The code that I currently have works, but I'm wondering if there is a better way.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;

using System.Diagnostics;
using System.Threading.Tasks;

namespace TimerStudy
{
    public class App : Application
    {
        public TimeSpan tsCumulative;
        public TimeSpan tsSecond = new TimeSpan(0, 0, 1);
        public bool bRunning = false;

        Label lblTimerText;
        Button btnClicker;

        public App ()
        {
            // The root page of your application

            lblTimerText =
                        new Label
                        {
                            XAlign = TextAlignment.Center,
                            Text = "00:00:00"
                        };

            btnClicker = new Button
            {
                Text = "Start",
                Font = Font.SystemFontOfSize(NamedSize.Large),
                BorderWidth = 1,
                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.CenterAndExpand
            };
            btnClicker.Clicked += OnButtonClicked;

            MainPage = new ContentPage {
                Content = new StackLayout {
                    VerticalOptions = LayoutOptions.Center,
                    Children = {
                        lblTimerText,
                        btnClicker
                    }
                }
            };
        }

        void OnButtonClicked(object sender, EventArgs e)
        {
            if (bRunning)
            {
                //Timer is Running
                bRunning = false;
                btnClicker.Text = "Start";
            }
            else
            {
                //Timer is Stopped
                bRunning = true;
                btnClicker.Text = "Stop";
                TimerRunning();
            }
        }

        async Task TimerRunning()
        {
            while (bRunning)
            {
                await Task.Delay(1000);
                tsCumulative = tsCumulative + tsSecond;
                lblTimerText.Text = tsCumulative.ToString();
            }
        }

        protected override void OnStart ()
        {
            // Handle when your app starts
        }

        protected override void OnSleep ()
        {
            // Handle when your app sleeps
        }

        protected override void OnResume ()
        {
            // Handle when your app resumes
        }
    }
}

The first issue comes in the form of a warning @ line 66 in OnButtonClicked for TimerRunning().

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

The second issue is that when I click the stop button, most of the time, the counter rolls forward an additional second. My guess is that this is a result of how I have the timer implemented.

Suggestions appreciated.


Viewing all articles
Browse latest Browse all 58056

Trending Articles