簡體   English   中英

重置倒數計時器

[英]Reset CountDown Timer

沒有 EventHandler (timerIdle_Tick),計時器不會啟動:

timerIdle.Tick += new EventHandler(timerIdle_Tick);

按照您的指示后的計時器代碼:

class Timers
{
    public static System.Windows.Forms.Label lblIdle;
    public static Timer timerIdle = new Timer();
    public static int segundo = 0;

    // TIMER IDLE
    public static void timerIdleOn()
    {
        segundo = 300;
        timerIdle.Interval = 1000;
        timerIdle.Start();
    }
    public static void timerIdleOff()
    {
        timerIdle.Stop();
    }

    public static void timerIdle_Tick(object sender, EventArgs e)
    {
        segundo--;

        if (segundo == 0)
        {
            timerIdle.Stop();
            dgView.addLinha(2, "config_Teste", "TIMER ACABOU", 0);
        }
        else
            dgView.addLinha(2, "config_Teste", TimeSpan.FromSeconds(segundo).ToString(@"mm\:ss"), 0);
    }
}

它跟着定時器開始的地方,我不使用按鈕,只是暫時測試方法,之后就不存在了。

namespace HC
{
    public partial class frm_console : Form
    {
        public frm_console()
        {
            InitializeComponent();
            dgView.DataConsole = this.DataConsol;           
            Timers.timerIdleOn();
        }
    }
}

對此:

timerIdle.Stop();
timerIdle.Start();

Timer object 對計時一無所知。 這只是一種允許您的timerIdle_Tick()方法定期調用的機制。 因此,很自然地,簡單地再次停止和啟動計時器對計時器的顯示時間完全沒有影響。

與您之前關於該程序的問題一樣,您仍然沒有提供最小、完整和可驗證的代碼示例 所以不可能確定需要什么。

但是查看您發布的代碼,在我看來,您可以簡單地為您的tempo變量分配一個新值,以便在您想要的任何時間重新開始計數。 例如:

timerIdle.Stop();
tempo = 300; // 300 seconds, or in other words, 5 minutes
timerIdle.Start();

對於它的價值,您的代碼可以簡化。 好吧,暫時忽略“在每個滴答聲上遞減一個秒計數器”只是實現這一點的錯誤方法(正如您在上一個問題中向您解釋的那樣),您的Tick處理程序方法的主體可能看起來更像這樣並且仍然做同樣的事情:

public static void timerIdle_Tick(object sender, EventArgs e)
{
    tempo--; // decrement first, then segundo and minuto already will be automatically decremented correctly

    // NOTE: it's not clear whether these are even used outside this method.
    // If not, just make them locals and do this computation only in the
    // "else" block below.
    minuto = tempo / 60; // will be 0 if tempo < 60
    segundo = tempo % 60; // will be tempo if tempo < 60

    if (tempo < 0)
    {
        timerIdle.Stop();

        // WARNING: this renders the timerIdle object invalid for future use.
        // If you try to restart the timer without creating a new one after this
        // statement executes, an exception will be thrown. If you intend to ever
        // reuse timerIdle, you should not dispose it here.
        timerIdle.Dispose();

        dgView.addLinha(2, "config_Teste", "TIMER ACABOU", 0);
    }
    else
    {
        // no need to recombine minuto and segundo back into tempo,
        // since tempo was already decremented to start with and
        // continues to have the right value here.

        string temp = string.Format($"{minuto:D2}:{segundo:D2}");
        lblIdle.Text = temp;
        dgView.addLinha(2, "config_Teste", temp, 0);
    }
}

這更像是我期望看到的。 計時器正在frm_console class 內部創建和處理。 請注意,所有static已被刪除:

public partial class frm_console : Form
{

    public int segundo = 300;
    public Timer timerIdle = new Timer();   
    
    public frm_console()
    {
        InitializeComponent();
        dgView.DataConsole = this.DataConsol;
        timerIdle.Interval = 1000; 
        timerIdle.Tick += new EventHandler(timerIdle_Tick);        
        timerIdle.Start();
    }
    
    private void btnReset_Click(object sender, EventArgs e)
    {
        segundo = 300; // reset to 5 minutes
        timerIdle.Start();
    }

    private void btnPauseResume_Click(object sender, EventArgs e)
    {
        if (timerIdle.Enabled)
        {
            timerIdle.Stop();
        }
        else if (segundo > 0)
        {
            timerIdle.Start();
        }
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        timerIdle.Stop();
    }   

    public void timerIdle_Tick(object sender, EventArgs e)
    {
        segundo--;
        
        if (segundo == 0)
        {
            timerIdle.Stop();
            dgView.addLinha(2, "config_Teste", "TIMER ACABOU", 0);
        }
        else
        {
            dgView.addLinha(2, "config_Teste", TimeSpan.FromSeconds(segundo).ToString(@"mm\:ss"), 0);
        }
    }
            
}

以“btn”開頭的方法是按鈕單擊處理程序,但它們可以轉換為常規方法。 你應該能夠理解他們在做什么。

你試過這個:

  timerIdle.Enabled = false;
  timerIdle.Enabled = true;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM