简体   繁体   English

C#倒数计时器暂停

[英]c# countdown timer pause

I've been trying to create a countdown timer(from 30 minutes) in ac# winforms application that has the ability to pause, and then resume from where the pause took place. 我一直在尝试在具有暂停功能的ac#winforms应用程序中创建一个倒计时计时器(从30分钟开始),然后从发生暂停的地方恢复。 I've tried a number of solutions to achieve this as I know there isn't a specific pause function of the System.Windows.Forms.Timer. 我已经尝试了多种解决方案来实现此目的,因为我知道System.Windows.Forms.Timer没有特定的暂停功能。 I've scoured the internet but couldn't find anything that I could apply to my scenario. 我搜寻了互联网,但找不到适合我的方案的任何内容。 Everything I've tried either results in the timer starting again from 30 minutes, or continuing from where it would have been without a pause. 我尝试过的所有操作都会导致计时器从30分钟重新开始,或者从原本可以不暂停的位置继续进行。 This is driving me mad. 这让我发疯。 Can anybody help or suggest an alternative way of doing this? 任何人都可以帮助或建议替代方法吗? This is my first post here so apologies if I've made any errors. 这是我在这里的第一篇文章,如果出现任何错误,我们深表歉意。 Code below. 下面的代码。 I've commented out the code that is causing me an issue - I know it's not correct, either syntactically or logically. 我已经注释掉导致我出现问题的代码-我知道从语法或逻辑上讲这都是不正确的。

public partial class FormWithTimer : Form
{
    System.Windows.Forms.Timer timerx = new System.Windows.Forms.Timer();

    DateTime startTime = DateTime.Now;
    DateTime stopTime = DateTime.Now;

    public FormWithTimer()
    {
        InitializeComponent();
        TimerLabel.Text = "30:00";
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    void timer_Tick(object sender, EventArgs e)
    {

    }

    public void StartButton_Click(object sender, EventArgs e)
    {
        startTime = DateTime.Now;

        BeginTextBox.Text = startTime.ToString();


        TimerLabel.Visible = true;
        timerx.Tick += (obj, args) =>
            TimerLabel.Text = (TimeSpan.FromMinutes(30) - (DateTime.Now - startTime)).ToString("mm\\:ss");

        timerx.Enabled = true;

    }

    public void PauseButton_Click(object sender, EventArgs e)
    {

        if (PauseButton.Text == "Pause")
        {
            timerx.Stop();
            PauseButton.Text = "Start";
            stopTime = DateTime.Now;
        }
        else
        {
            PauseButton.Text = "Pause";

            timerx.Start();
            TimerLabel.Visible = true;
            //timerx.Tick += (obj, args) =>
            //    TimerLabel.Text = (TimeSpan.FromMinutes(30) - (DateTime.Now - (startTime - stopTime))).ToString("mm\\:ss");

            timerx.Enabled = true;
        }

    }

}

I guess you are trying to have the timer continue where it was when it got paused. 我想您正在尝试使计时器继续暂停时的位置。

To do that, I would suggest using stopTime for that matter: 为此,我建议为此使用stopTime

In your "Pause-Unpause"- Routine: 在您的“暂停-暂停”程序中:

public void PauseButton_Click(object sender, EventArgs e)
{

    if (PauseButton.Text == "Pause")
    {
        timerx.Stop();
        PauseButton.Text = "Start";
        stopTime = DateTime.Now;
    }
    else
    {
        PauseButton.Text = "Pause";
        startTime += (DateTime.Now - stopTime);

        timerx.Start();
        TimerLabel.Visible = true;

        timerx.Enabled = true;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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