如何在c#中暂停进度条

how to pause progressbar in c#

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

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

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

计算当前开始日期(在进度开始前获取)和结束日期(每次进入 timer1_Tick 时获取)

DateTime startTime = DateTime.Now;


private void timer1_Tick(object sender, EventArgs e)
    {
DateTime endTime = DateTime.Now

TimeSpan span = endTime.Subtract( startTime );
if(span.Minutes % 5 == 0){
        circularProgressBar1.Increment(1);
        circularProgressBar1.Text =    circularProgressBar1.Value.ToString();
        circularProgressBar1.SuperscriptText = "%";
 }
}

小时;

    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}
    }

我建议你记住开始时间,并用它来计算每个 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 = "%";
    }
}

您可以使用 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 = "%";
}