具有迭代的winform中的计时器

时间:2011-03-21 09:40:16

标签: c# winforms timer iteration

       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。迭代将以快速的方式结束。如何使用计时器设置迭代以使用计时器执行每秒?


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

    }

它完成了工作,但我遇到的问题是在循环结束后,存在的碎片消失了。是因为背景工作者,没有它的原因,我没有任何碎片在过程结束时消失。第二次激活方法时,它发生了

3 个答案:

答案 0 :(得分:2)

在后台线程中运行它并将Thread.Sleep(1000)放入循环中。

这种方式基于时间,不会冻结您的应用。

答案 1 :(得分: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);
    }
}

答案 2 :(得分:0)

就个人而言,我会把它放到一个单独的线程中并使用Thread.sleep()。结合访问GUI元素,我建议使用BackgroundWorker(参见MSDN,易于实现)。

如果你坚持使用计时器,我建议你使用'Ed S.'的建议。计时器也可以放在您的表单上,而不是在代码中创建它。正是你喜欢的。