如何在c#中每秒刷新一个字符串

本文关键字:字符串 一个 刷新 | 更新日期: 2023-09-27 18:02:17

我想知道是否有一种方法可以每1000毫秒刷新进度条,因为当我运行我的程序时,我必须点击进度条以获得反馈。

private void progressBar1_Click(object sender, EventArgs e)
    {
        Application.EnableVisualStyles();
        progressBar1.Style = ProgressBarStyle.Continuous;
        progressBar1.Value = (int)(power.BatteryLifePercent * 100);
        label1.Text = string.Format("{0}%", (power.BatteryLifePercent * 100));
    }

如何在c#中每秒刷新一个字符串

每隔X个时间间隔执行一个任务通常使用Timer

public void Form_Load(object sender, EventArgs e)
{
    // Moved here, supposing that you don't not needed to
    // set them to same value every second....
    Application.EnableVisualStyles();
    progressBar1.Style = ProgressBarStyle.Continuous;
    System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
    t.Interval = 1000;
    t.Tick += timeElapsed;
    t.Start();
}
private void timeElapsed(object sender, EventArgs e)
{
    progressBar1.Value = (int)(power.BatteryLifePercent * 100);
    label1.Text = string.Format("{0}%", (power.BatteryLifePercent * 100));
}