Code Bye

c# 倒计时问题

有短代码如下
        int count = 5;
        private void CountDown()
        {
            while (true)
            {
                if (count == 0) { return; }
                count -= 1;
                lbl_Time.Text = count.ToString();
                if (count == 0)
                {
                    lbl_Time.Visible = false;
                }
                Thread.CurrentThread.Join(1000);
            }
        }

外部方法给count 负值 他就每秒 -1
现遇到问题描述如下:
Thread.CurrentThread.Join(1000); 这个延时是1秒 当它阻止到 0.9秒的时候给 count 重新负值 5
那么过了 0.1 秒 5 就减到 4 了, 这个时间就不是 间隔一秒钟了.
本人现在需要的是效果是:
变量负值-> 每秒 -1 ,减到 0 停止.
在变量任何值的时候给变量重新负值,都要从负值开始间隔一秒  新变量值 -1  减到 0 停止,
不用 Timer 控件.
有没有好办法 .
急需,各位高手帮帮忙.

解决方案

10

   public partial class Form2 : Form
    {
  
        CancellationTokenSource tokenSource = new CancellationTokenSource();
        // Fields...
        private int _Count;
        public int count
        {
            get { return _Count; }
            set
            {
                _Count = value;
                tokenSource.Cancel();
                tokenSource = new CancellationTokenSource();
                CountDown();
            }
        }
      
        
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
          
            this.count = Convert.ToInt32(this.textBox1.Text);
        }
        
        void CountDown()
        {
            SynchronizationContext UIcontext = SynchronizationContext.Current;
            var CancelToken=   tokenSource.Token;
            Task t = new Task(() =>
            {
                while (!CancelToken.IsCancellationRequested&&count>-1)
                {
                    UIcontext.Post((p) => {
                       
                        this.label1.Text = _Count.ToString(); 
                        _Count--;
                    }, null);
                    Thread.CurrentThread.Join(1000);
                }
            });
            t.Start();
        }
    }

30

    public partial class Form1 : Form
    {
        int count = 0;
        System.Threading.Timer timerThr;
        private delegate void SetTBMethodInvoke(object state);
        public Form1()
        {
            InitializeComponent();
            timerThr = new System.Threading.Timer(new TimerCallback(SetTB), null, Timeout.Infinite, 500);
        }
        public void SetTB(object value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetTBMethodInvoke(SetTB), value);
            }
            else
            {
                count -= 1;
                this.textBox1.Text = count.ToString();
                if(count == 0)
                {
                }
            }
        }
        private void btStart_Click(object sender, EventArgs e)
        {
            count = 6;
            timerThr.Change(0, 1000);
        }
        private void btStop_Click(object sender, EventArgs e)
        {
            timerThr.Change(Timeout.Infinite, 1000);
        }
    }

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明c# 倒计时问题