如何在c#中暂停进度条

时间:2017-02-23 07:26:52

标签: c# timer progress-bar

我有一个进度条,可在20分钟内完成。我使用计时器控制在进度条中递增。现在我希望每隔5分钟暂停2分钟,这些分钟内没有增加进度,2分钟后再次恢复。怎么做?这是我的代码

private void timer1_Tick(object sender, EventArgs e)
{
    circularProgressBar1.Increment(1);
    circularProgressBar1.Text = circularProgressBar1.Value.ToString();
    circularProgressBar1.SuperscriptText = "%";
}

这个工作正常,但是如何在每5分钟后自动暂停一次。

3 个答案:

答案 0 :(得分:1)

我建议你记住开始时间并用它来计算每个Tick的经过时间。然后你可以检查你是否在休息时间间隔之外,即如果你在第一次休息之前(elapsed.Minutes < activeMinutes)或者你是在第一次休息之后(elapsed.Minutes >= activeMinutes + pauseMinutes不在另一个休息间隔(elapsed.Minutes % (activeMinutes + pauseMinutes) < activeMinutes))。

完整的代码看起来像这样:

private DateTime _startTime;
private int activeMinutes = 5;
private int pauseMinutes = 2;
private Timer _timer;

private void StartTimer()
{
    if(_timer != null)
    {
        // detach event handler from old timer before creating a new one
        _timer.Tick -= timer1_Tick;
    }
    _timer = new Timer();
    _timer.Interval = 12000;
    _timer.Tick += timer1_Tick;
    _timer.Start();
    _startTime = DateTime.Now;
}

private void timer1_Tick(object sender, EventArgs e)
{    
    TimeSpan elapsed = DateTime.Now.Subtract( _startTime );
    if( elapsed.Minutes < activeMinutes ||
       (elapsed.Minutes >= activeMinutes + pauseMinutes &&
        elapsed.Minutes % (activeMinutes + pauseMinutes) < activeMinutes))
    {
        circularProgressBar1.Increment(1);
        circularProgressBar1.Text = circularProgressBar1.Value.ToString();
        circularProgressBar1.SuperscriptText = "%";
    }
}

答案 1 :(得分:0)

您可以使用TimeSpan并在每个计时器滴答时为此TimeSpan添加间隔。当你达到5分钟并重新开始时修改计时器。这里是示例,它未经过测试,因此可能需要进行一些调试。

我建议将计时器的初始间隔设置为1000毫秒,以便每秒更新进度条,尽管您也可以设置更长的间隔。

private double interval = 1000; // one second interval so progress bar updates every second.
private TimeSpan elapsed; // time span that reaches 5 minutes. this will reset to 0 after 5 minutes.
private TimeSpan totalElapsed; // total time passed. used to set value of progress bar
private bool progressPaused; // flag, true if progressbar is at 2 min pause, otherwise false

private void timer1_Tick(object sender, EventArgs e)
{
    const int fiveMinutes = 5*1000*60;
    const int twoMinutes = 2*1000*60;
    const int totalMinutes = 20*1000*60;

    var timer = (Timer) sender; // assuming Timer is System.Timers.Timer

    if(timer.Enabled == false) return;

    if (progressPaused) 
    {
        // progressbar was paused. prepare to start again

        timer.Enabled = false;
        timer.AutoReset = true;
        timer.Interval = interval; // previous interval
        progressPaused = false;
        timer.Enabled = true;
        return;
    }

    // add interval of timer to TimeSpans
    totalElapsed = totalElapsed.Add(TimeSpan.FromMilliseconds(timer.Interval));
    elapsed = elapsed.Add(TimeSpan.FromMilliseconds(timer.Interval));

    if (elapsed.Milliseconds > fiveMinutes) // if we reached 5 minutes
    {
        elapsed = default(TimeSpan); // reset timespan from 5 min to 0
        timer.Enabled = false;
        timer.AutoReset = false; // auto reset should be off because we only pause once per 2 minutes. (although this may have no effect)
        interval = timer.Interval;
        timer.Interval = twoMinutes; // two minute pause
        progressPaused = true; // set falg
        timer.Enabled = true;
    }

    circularProgressBar1.Value = (int)(totalElapsed.Milliseconds/(double)totalMinutes*100); // calculate new progressbar value. (elapsed/total*100)
    circularProgressBar1.Text = circularProgressBar1.Value.ToString();
    circularProgressBar1.SuperscriptText = "%";
}

答案 2 :(得分:-1)

计时器计时器;

    int countTime;
    bool breakReached;
    int breakTime;
    int breakLength;
    int countToFinish;
    int finishTime;

    public Form1()
    {
        InitializeComponent();
        timer = new Timer();
        timer.Interval = 1000;

        breakTime = 300; // in sek
        breakLength = 120; // in sek
        finishtime = 1200;
        breakReached = false;
        timer.Tick += Timer_Tick;
        timer.Start();
    }

    private void Timer_Tick(object sender, EventArgs e)
    {
        if (countTime != breakTime && breakReached != true)
        {
            progressBar1.Increment(1);
            progressBar1.Text = progressBar1.Value.ToString();
        }
        else
        {
            //break was reached
            breakReached = true;
            countTime = 0;
            while(countTime != breakLength)
            {
                //do the 2 min break
            }
            breakReached = false;
        }
        countTime++;
        countToFinish ++;
        if(countToFinish == finishTime)
        {//its done}
    }