2011-03-21 20 views
-1
 foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2 
     {   
      makeSelfMoves = replay[item.Key]; 
      codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]); 
      PrintPieces(codeFile.PieceState()); 
      // MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] + "____a is: " + a); 

     } 

我想執行這整個迭代,我負責遊戲重播,以1秒爲間隔。我在我的表格中放置了一個計時器,並將其設置爲1秒(這應該使得片段以1秒的間隔移動)。我做了一個事件,並在循環之前,我把語句,timer1.enabled = true。迭代將以快速方式結束。如何使用定時器設置迭代以使用定時器執行每秒?定時器在winform中迭代


public void ReplayGame() 
    { 
     Class2.replayIsOn = true; 
     replay=serializeMeh.giveBackDictionary(); 
     int[] makeSelfMoves=new int[4]; 


     //Timer t = new Timer(); 
     //t.Interval = 1000; 
     //t.Tick += timer1_Tick; 
     //t.Enabled = true; 
     //t.Start(); 
     if (backgroundWorker1.IsBusy != true) 
     { 
      // Start the asynchronous operation. 
      backgroundWorker1.RunWorkerAsync(); 
     } 

     //foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2 
     //{ 


     // makeSelfMoves = replay[item.Key]; 
     // codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]); 
     // PrintPieces(codeFile.PieceState()); 


     // MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3]); 

     //} 


    } 

如果我想重播上述方法被激活。

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) 
    { 
     BackgroundWorker worker = sender as BackgroundWorker; 

     int[] makeSelfMoves = new int[4]; 

     foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2 
     { 
      makeSelfMoves = replay[item.Key];// delivers an array of a single move location startrow,startcolumn,endrow,endcolun 
      codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]); 
      PrintPieces(codeFile.PieceState());// prints the code on the board 

      System.Threading.Thread.Sleep(1000); 
     } 

    } 

它做的工作,但我有問題是循環結束後,就存在消失的作品。是因爲背景工作者,沒有它的原因,我沒有在工作結束時消失。第二次,我激活該方法,它發生

+0

目前還不清楚你想要做什麼。請寫更多。 – Morten 2011-03-21 09:46:33

回答

2

在後臺線程中運行它並將Thread.Sleep(1000)在循環中。

這樣,它將基於時間而不是凍結你的應用程序。

0

我們需要看到更多的代碼,但它是非常簡單的:

使用System.Windows.Forms的;

SomeMethod(...) 
{ 
    Timer t = new Timer(); 
    t.Interval = 1000; 
    t.Tick += t_Tick; 
    t.Enabled = true; 
    t.Start(); 
} 

void t_Tick(...) 
{ 
    foreach (KeyValuePair<int, int[]> item in replay)// count should be more than 2 
    {   
     makeSelfMoves = replay[item.Key]; 
     codeFile.ExecuteAll(makeSelfMoves[0], makeSelfMoves[1], makeSelfMoves[2], makeSelfMoves[3]); 
     PrintPieces(codeFile.PieceState()); 
     // MessageBox.Show("rowStart: " + makeSelfMoves[0] + ". rowEnd: " + makeSelfMoves[2] + ". columnStart: " + makeSelfMoves[1] + ". columnEnd: " + makeSelfMoves[3] + "____a is: " + a); 
    } 
} 
0

個人而言,我要把它放到一個單獨的線程,並使用Thread.sleep()方法。結合訪問GUI元素,我會建議使用BackgroundWorker(請參閱MSDN,易於實現)。

如果你堅持使用計時器,我建議你使用'Ed S.'的建議。計時器也可以放在你的表格上,而不是用代碼創建它。只是你喜歡什麼。

+0

我知道後臺工作人員,我以爲我可以用計時器做到這一點。我把計時器放在窗體上,但它表現奇怪,看到我的代碼如下 – 2011-03-21 11:31:34

+0

我得到invalidOperationException,Object當前在其他地方使用。 – 2011-03-21 12:12:18

+0

例外是在主要靜態void Main()中的Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ChessBoard()); //在這裏 } – 2011-03-21 12:12:47