2015-07-11 36 views
0

我想知道是否有每1000毫秒刷新进度栏的方法,因为当我运行我的程序时,我必须单击进度栏才能从中获取反馈。如何在C#中每秒刷新一次字符串

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

这是我到目前为止。

回答

2

执行使用定时器

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)); 
} 
+0

好的任务每隔X时间间隔通常被解决。感谢那。我会试试看,并告诉你它是否工作。 – Sachin

+0

还在等待,知道它是否已经工作 – Steve

+0

不。它没有工作。抱歉。 – Sachin