如何以异步方式递增计时器? [英] How to Increment timer asynchronously ?

查看:104
本文介绍了如何以异步方式递增计时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想异步更新计时器点击一个按钮。

说比如我设置的时间= 60秒。

和当我运行过一段时间后,计时器已经达到了45秒的节目,当我按一下按钮,那么它应该J =增加15秒的时候,定时器应异步更改为60秒。请帮助

 私人INT时间= 60;
    DateTime的DT =新的DateTime();
    私人J = 15;
    私人DispatcherTimer定时器;    公共主窗口()
    {
        的InitializeComponent();
        定时器=新DispatcherTimer();
        timer.Interval =新时间跨度(0,0,1);
        timer.Tick + = timer_tick;
        timer.Start();
        }    无效timer_tick(对象发件人,EventArgs的发送)
    {
        如果(时间> 0)
        {
          时间 - ;
            text.Text = TimeSpan.FromSeconds(时间)的ToString();
        }
        其他
        {
            timer.Stop();
        }
    }  私人无效Button_Click(对象发件人,RoutedEventArgs E)
    {
        text.Text = dt.AddSeconds(J)的ToString(HH:MM:SS);
    }


解决方案

下面是我的code你可以尝试一下它的工作。

 私人INT时间= 60;
    DateTime的DT =新的DateTime();
    私人INT J = 15;
    私人定时器定时器1 =新的Timer();
    无效timer_tick(对象发件人,EventArgs的发送)
    {
        如果(时间> 0)
        {
            时间 - ;
            text.Text = TimeSpan.FromSeconds(时间)的ToString();
        }
        其他
        {
            timer1.Stop();
        }
    }    公共计时器()
    {
        的InitializeComponent();
        定时器1 =新的Timer();
        timer1.Interval = 1000;
        timer1.Tick + = timer_tick;
        timer1.Start();
    }    私人无效的button1_Click(对象发件人,EventArgs的发送)
    {
        时间+ = j的;
    }

I am trying to Update a timer asynchronously On a Button Click .

say example i have set the time = 60 seconds

and when i run the program after few TIME the timer has reached to 45 seconds and when i click the Button ,then it should add j=15 seconds to the time and the timer should change to 60 seconds asynchronously. Please Help

    private int time = 60;
    DateTime dt = new DateTime();
    private j = 15 ;
    private DispatcherTimer timer;

    public MainWindow()
    {
        InitializeComponent();
        timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0, 0, 1);
        timer.Tick += timer_tick;
        timer.Start();
        }

    void timer_tick(object sender, EventArgs e)
    {
        if (time >0)
        {
          time--;
            text.Text = TimeSpan.FromSeconds(time).ToString();
        }
        else
        {
            timer.Stop();
        }
    }

  private void Button_Click(object sender, RoutedEventArgs e)
    {
        text.Text = dt.AddSeconds(j).ToString("HH:mm:ss");
    }

解决方案

Here is my code you can try it it's working.

    private int time = 60;
    DateTime dt = new DateTime();
    private int j = 15;
    private Timer timer1 = new Timer();


    void timer_tick(object sender, EventArgs e)
    {
        if (time > 0)
        {
            time--;
            text.Text = TimeSpan.FromSeconds(time).ToString();
        }
        else
        {
            timer1.Stop();
        }
    }

    public timer()
    {
        InitializeComponent();
        timer1 = new Timer();
        timer1.Interval = 1000;
        timer1.Tick += timer_tick;
        timer1.Start();
    }



    private void button1_Click(object sender, EventArgs e)
    {
        time += j;
    }

这篇关于如何以异步方式递增计时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆