简体   繁体   中英

How to make a countdown timer?

Hi I'm writing a Scoreboard UWP app and I would like to know how to make the code behind the timer. Because is a basketball scoreboard it has 2 clocks, one only for seconds (shotclock) and other that manages minutes and seconds. So, I would like to know if there is an easy way to make this kind of countdowns in UWP.

I've just found this but it counts up not down:

    private void stopwatch_Tapped(object sender, TappedRoutedEventArgs e)
    {
        if (_stopwatch.IsRunning)
        {
            _stopwatch.Stop();
            _timer.Dispose();
        }
        else
        {
            _stopwatch.Start();
            _timer = new Timer(updateTime, null, (int)TimeSpan.FromMinutes(1).TotalMinutes, Timeout.Infinite);
        }

    }

    private async void updateTime(object state)
    {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
            () =>
                {

                    stopwatchLbl.Text = String.Format("{0:00}:{1:00}:{2:00}", _stopwatch.Elapsed.TotalMinutes, _stopwatch.Elapsed.TotalSeconds, _stopwatch.Elapsed.TotalMilliseconds / 10);

                    //stopwatchLbl.Text = "00:00:00";

                }
            );

    }

try this code , i wish that will help you :)

internal class countDownTimer
    {
        public int enlapsedTime;
        private DispatcherTimer dispatch;

        public delegate void MyCallback();
        public delegate void MyCallback2(int value);
        public MyCallback OnStartTime;
        public MyCallback OnStopTime;
        public MyCallback OnEndTime;
        public MyCallback2 OnCountTime;

        public countDownTimer()
        {
            Debug.WriteLine("StopWatch init");
            enlapsedTime = 0;
            dispatch = new DispatcherTimer();
            dispatch.Interval = new TimeSpan(0, 0, 1);
            dispatch.Tick += timer_Tick;
        }

        private void timer_Tick(object sender, object e)
        {
            enlapsedTime--;
            Debug.WriteLine(enlapsedTime);

            if (OnCountTime != null) OnCountTime(enlapsedTime);

            if (enlapsedTime < 0)
            {
                enlapsedTime = 0;
                dispatch.Stop();
                if (OnEndTime != null) OnEndTime();
            }
        }

        public void Start()
        {
            dispatch.Start();
            if (OnStartTime != null) OnStartTime();
            Debug.WriteLine("iniciar contador");
        }

        public void Stop()
        {
            dispatch.Stop();
            if (OnStopTime != null) OnStopTime();
            Debug.WriteLine("parar contador");
        }

        public bool IsEnabled
        {
            get
            {
                return dispatch.IsEnabled;
            }
        }

    }

You may use the UWPHelper.Utilities.ThreadPoolTimer from my NuGet package UWPHelper . You will need to check the Include pre-release checkbox in NuGet Package Manager to be able to download it.

My ThreadPoolTimer is a wrapper class for System.Threading.Timer and it works similarly to the DispatcherTimer however it runs on the ThreadPool, not on the UI thread.

using UWPHelper.Utilities;

// TimeSpan indicates the interval of the timer
ThreadPoolTimer timer = new ThreadPoolTimer(TimeSpan.FromSeconds(1));
timer.Tick += OnTick;

void OnTick(object sender, EventArgs e)
{
    // Method invoked on Tick - every second in this scenario
} 

// To start the timer
timer.Start();

// To stop the timer
timer.Stop();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM