private async void RunAsync()
    {
        if (_cancelSource != null)
        {
            _cancelSource.Cancel();
        }
        _cancelSource = new CancellationTokenSource();
        var token = _cancelSource.Token;

        await Task.Run(() =>
        {
            while (_targetTime > DateTime.Now)
            {
                if (token.IsCancellationRequested)
                {
                    return;
                }
            }
            ;
            _synchronizationContext.Post((s) => OnTimerUp?.Invoke(), null);
        }, token);
    }
Esempio n. 2
0
    // Update is called once per frame
    void Update()
    {
        if (Active)
        {
            countdownTime -= Time.deltaTime;
            int    minutes  = Mathf.FloorToInt(countdownTime / 60F);
            int    seconds  = Mathf.FloorToInt(countdownTime - minutes * 60);
            string niceTime = string.Format("{0:0}:{1:00}", minutes, seconds);
            TimerText.text = niceTime;

            if (countdownTime <= 0)
            {
                Active         = false;
                countdownTime  = 0;
                minutes        = 0;
                seconds        = 0;
                Time.timeScale = 0;

                timerUp.Invoke();
            }
        }
    }