2013-04-20 45 views
1

我的應用程序運行文件,每個文件都有自己的運行時間。 在毫秒這一功能得到了進步時應該運行的時間:ProgressBar運行速度超過定義

timerProgress = my timer 
pbStatus = my progress bar 

public void AnimateProgBar(int milliSeconds) 
{ 
    if (!timerProgress.Enabled && milliSeconds != 0) 
    { 
     pbStatus.Value = 0;; 
     timerProgress.Interval = milliSeconds/100; 
     timerProgress.Enabled = true; 
    } 
} 

,這是我的計時器,填補進度條:

private void timerProgress_Tick(object sender, EventArgs e) 
    { 
     if (pbStatus.Value < 100) 
     { 
      pbStatus.Value += 1; 
      pbStatus.Refresh(); 
     } 
     else 
     { 
      timerProgress.Enabled = false; 
     } 
    } 

我的問題是進度條跑太快了,例如,如果AnimateProgBar的值爲12000(12秒),進度條只運行6-7秒。

+1

你將(毫秒/ 100)的總毫秒數分開。這就是爲什麼它跑得比你想要的快。 – 2013-04-20 15:16:28

+0

我假設你的進度條的最小值和最大值是0和100,step = 1? – Yeronimo 2013-04-20 15:17:24

+0

yes,min = 0,max = 100,step = 1,如何解決? – user2214609 2013-04-20 15:19:12

回答

3

這是腥說你的代碼不起作用。我嘗試了幾次,每次都錯過了大約0.6秒;它看起來像該計時器只是不精確

你可以做的就是照顧自己的時間,而不是信任定時器:

WithEvents Tmr As New Timer With {.Interval = 100} 
Dim startTime As Date, AnimationTime% 

Sub AnimateProgress(ms%) 
    If ms <= 0 Then Exit Sub 

    ProgressBar1.Value = 0 
    AnimationTime = ms 
    startTime = Now 

    Tmr.Start() 
End Sub 

Private Sub Tmr_Tick() Handles Tmr.Tick 
    ProgressBar1.Value = Math.Min((Now - startTime).TotalMilliseconds/AnimationTime, 1) * 100 
    If ProgressBar1.Value = 100 Then Tmr.Stop() 
End Sub 

編輯 - 響應波紋管的答覆:

噢,對不起,沒有它是vb.net。我也很瞭解這兩種語言,但我更喜歡vb,並傾向於認爲其他人也這樣做。

這裏是C#版本:

DateTime startTime; int animationTime; 

void AnimateProgress(int ms) { 
    if (ms <= 0) return; 

    progressBar1.Value = 0; 
    animationTime = ms; 
    startTime = DateTime.Now; 

    Tmr.Start(); 
} 

private void Tmr_Tick(object sender, EventArgs e) { 
    progressBar1.Value = (int)(Math.Min((DateTime.Now - startTime).TotalMilliseconds/animationTime, 1) * 100); 
    if (progressBar1.Value == 100) Tmr.Stop(); 
} 
+1

的示例,這是C#....? – user2214609 2013-04-20 16:30:49

+2

@ user2214609他更新了線程。不要粗魯...他努力幫助你。 – 2013-04-20 18:04:49

+0

非常感謝您的幫助! – user2214609 2013-04-21 09:50:14

0

你可以用這個樣本嘗試,基於PerformStep method

var progressBar = new System.Windows.Forms.ProgressBar(); 
progressBar.Maximum = 100; 
progressBar.Minimum = 0; 
progressBar.Step = 10; 

//begin loop 

//Your treatment of step 
progressBar.PerformStep(); 

//end loop 

MSDN鏈接:http://msdn.microsoft.com/fr-fr/library/system.windows.forms.progressbar.performstep(v=vs.80).aspx

您有樣品這裏

private void CopyWithProgress(string[] filenames) 
{ 
    // Display the ProgressBar control. 
    pBar1.Visible = true; 
    // Set Minimum to 1 to represent the first file being copied. 
    pBar1.Minimum = 1; 
    // Set Maximum to the total number of files to copy. 
    pBar1.Maximum = filenames.Length; 
    // Set the initial value of the ProgressBar. 
    pBar1.Value = 1; 
    // Set the Step property to a value of 1 to represent each file being copied. 
    pBar1.Step = 1; 

    // Loop through all files to copy. 
    for (int x = 1; x <= filenames.Length; x++) 
    { 
     // Copy the file and increment the ProgressBar if successful. 
     if(CopyFile(filenames[x-1]) == true) 
     { 
      // Perform the increment on the ProgressBar. 
      pBar1.PerformStep(); 
     } 
    } 
} 

鏈接:http://msdn.microsoft.com/fr-fr/library/system.windows.forms.progressbar.performstep(v=vs.80).aspx

+0

如何在代碼中使用它? – user2214609 2013-04-20 15:35:34

+0

foreach終止步驟add rogressBar.PerformStep(); ,前四行,你在你的方法的頂部設置初始化你的行爲 – 2013-04-20 15:38:04

+0

所有這些代碼在我的AnimateProgBar方法?我的計時器呢?你在說什麼循環? – user2214609 2013-04-20 15:41:41

0

我無法重現您的問題。我只是測試一種新的形式,與進度和定時器,你在你的問題詳細,而只是增加了一個按鈕,啓動了測試,並且一個標籤來顯示經過時間:

DateTime start; 
    private void button1_Click(object sender, EventArgs e) 
    { 
     start = DateTime.Now; 
     AnimateProgBar(12000); 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     label1.Text = DateTime.Now.Subtract(start).TotalSeconds.ToString(); 
     //the rest of your code, starting with "if (pbStatus.Value < 100)" 

我一直有12.6直到progressBar被填滿(並且計時器停止,凍結標籤文本)......可能是你的progressBar的一部分被隱藏了?

[編輯] 如果你好奇的0.6額外秒鶯也注意到了,那是因爲你設置的計時器的時間間隔毫秒至120毫秒,但計時器事件具有約18毫秒的分辨率,所以它會實際上是在126而不是120.