2011-12-19 76 views
6

好吧,所以我有這樣的問題。似乎無法獲得進度條動畫

public Class A{ 

    public A(){ 
     progressBar.Style = ProgressBarStyle.Marquee; 
     progressBar.MarqueeAnimationSpeed = 0;  
    } 

    public void DoSomething(){ 
     if(checkpasses){ 
      progressBar.MarqueeAnimationSpeed = 100; 
      //Do something here... 
      progressBar.MarqueeAnimationSpeed = 0; 
     } 
     else 
      //Do nothing... 
     } 
} 

問題是我的進度條不會開始移動。首先,我認爲它不會自己創建一個新的線程(我發現有線),所以我嘗試創建一個線程,但仍然是相同的結果。什麼都沒發生。這是我忘記的東西嗎?

+0

你想從另一個線程訪問progressBar嗎? – geniaz1 2011-12-19 22:15:52

+0

WinForms? WPF? SilverLight的?什麼?沒有像「C#ProgressBar」這樣的東西。 – 2011-12-19 22:20:29

+0

哦,對不起忘了提及,是的,它是WinForms。 – user1106784 2011-12-19 22:22:54

回答

6

你「在這裏做什麼」代碼將阻止用戶界面線程,這樣你就不會看到進度條更新,直到DoSomething的方法完成之後。當時你將動畫速度設置爲0.

嘗試在單獨的線程上放置「在此處執行某些操作」代碼。當該線程完成設置動畫速度回到0

事情是這樣的:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); 
     backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); 
    } 

    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
    { 
     progressBar1.MarqueeAnimationSpeed = 0; 
     progressBar1.Style = ProgressBarStyle.Blocks; 
     progressBar1.Value = progressBar1.Minimum; 
    } 

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     DoSomething(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     progressBar1.Style = ProgressBarStyle.Marquee; 
     progressBar1.MarqueeAnimationSpeed = 100; 
     backgroundWorker1.RunWorkerAsync(); 
    } 

    private void DoSomething() 
    { 
     Thread.Sleep(2000); 
    } 
} 
+0

啊!謝謝 – user1106784 2011-12-19 22:30:18

+0

我在任務中放置了「在此處做某事」的代碼,但仍未看到動畫的字幕。 – James 2017-06-22 19:50:06

0

我不知道這是否是最好的解決辦法,但我必須這樣說:

//this is the action item (button click) 
    private void importSFNFReportButton_Click(object sender, EventArgs e) 
    { //I run 
     backgroundWorker6Progress.RunWorkerAsync(); //this is how I start the progress bar 'movement' 
     bgwImportSF.RunWorkerAsync(); //this is another task that is lauchned after the progress bar is initiated 
    } 

這是實際的後臺工作

private void backgroundWorker6Progress_DoWork(object sender, DoWorkEventArgs e) 
    { 
     bool cont = true; 
     while (cont) 
     { 
      PauseForMilliSeconds(100); 
      updateProgressbar1(false); 
      if (noTasksExistCheck()) 
      { 
       updateProgressbar1(true); 
       cont = false; 
      } 
     } 
    } 

這是一個delegate-我就打電話給自動增加進度條指示器

delegate void updateProgressBarStatus(bool done); 
    private void updateProgressbar1(bool done) 
    { 
     if (progressBar1.InvokeRequired) 
     { 
      updateProgressBarStatus del = new updateProgressBarStatus(updateProgressbar1); 
      progressBar1.Invoke(del, new object[] { done }); 
     } 
     else 
     { 
      if (progressBar1.Value == progressBar1.Maximum) 
      { 
       progressBar1.Value = progressBar1.Minimum; 
      } 
      progressBar1.PerformStep(); 

      if (done == true) 
      { 
       progressBar1.Value = progressBar1.Minimum; 
      } 
     } 
    } 

我控制它通過必須檢查全球variba樂

noTasksExistCheck() 

這是計時器暫停

public static DateTime PauseForMilliSeconds(int MilliSecondsToPauseFor) 
    { 
     System.DateTime ThisMoment = System.DateTime.Now; 
     System.TimeSpan duration = new System.TimeSpan(0, 0, 0, 0, MilliSecondsToPauseFor); 
     System.DateTime AfterWards = ThisMoment.Add(duration); 
     while (AfterWards >= ThisMoment) 
     { 
      System.Windows.Forms.Application.DoEvents(); 
      ThisMoment = System.DateTime.Now; 
     } 
     return System.DateTime.Now; 
    } 
15

呼叫

Application.EnableVisualStyles(); 

在你的應用程序的開始。

+1

血腥的地獄沒有別的工作,直到這!謝謝!! – joshcomley 2013-04-19 19:51:10

+1

太棒了!謝謝,那也解決了我的問題。 – mbue 2013-06-03 07:58:44

+0

我在一個非常龐大的代碼庫中,遇到了這個問題。我認爲這是不可能的,但是在數百萬行代碼中,沒有人會這樣稱呼它。我在應用程序啓動和bam開始時添加了這一點,不確定的進度欄工作。謝謝! – Josh 2017-01-09 16:58:57

0

只是爲了補充一點,Dave建議的解決方案只有在康斯坦丁的建議代碼存在的情況下才有效。否則,您應該考慮在DoWork中通過以下代碼手動增加循環中的progressbar.value:BeginTimer()