2016-11-07 30 views
0

我想減慢foreach循環,所以我可以顯示gif,需要大約2秒才能完成,但foreach循環非常快,除了最後一個不能看到gif外。減慢foreach,所以我可以顯示所有GIFS

foreach (string s in words) 
     { 
      path = Dictionary.wordchecker(s);//Checks words for Animation 
      pictureBox1.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\images\\" + path + ".gif"); 
      textBox1.Text = s; 

     } 
+1

'Task.Delay( )'也許?沒有更多的信息我們無法真正瞭解。 – David

+0

你用什麼來顯示GIF? –

+0

請顯示更多代碼,突出顯示您如何顯示圖像。 – sr28

回答

2

按照要求我更新的解決方案與Windows窗體工作, 你可以做這樣的假設你有這樣的話字符串列表:

System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); 
List<string> words = new List<string>(); 

    int cur = 0; 

    public static int Main() 
    { 
     //2000 is two seconds you can adjust to the amount you need 
     timer.Interval = 2000; 
     timer.Tick += timerTick; 
     timer.Start(); 
    } 

    private void timerTick(object sender, object e) 
    { 
     if (cur < words.Count) 
     { 
      path = Dictionary.wordchecker(words[cur]);//Checks words for Animation 
      pictureBox1.Image = Image.FromFile(Directory.GetCurrentDirectory() + "\\images\\" + path + ".gif"); 
      textBox1.Text = s; 
      cur++; 
     } 
     else 
     { 
      timer.Stop(); 
     } 
    } 
+1

當你到達字符串列表的末尾時,怎麼樣?這將在第一次通過列表後引發異常。 – EJoshuaS

+1

是的編輯感謝,也在原來的職位,我沒有忘記啓動計時器也應該現在工作:) –

+0

是的,這應該解決這個問題,OP還可以做一個「環繞」,就像我在我的答案如果他想連續循環顯示圖像,或者他可以停止計時器,如果他只想像在這裏一樣顯示每個圖像。 – EJoshuaS

相關問題