Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# winform中带有迭代的计时器 foreach(重播中的KeyValuePair项)//计数应大于2 { makeSelfMoves=replay[item.Key]; ExecuteAll(makeSelfMoves[0]、makeSelfMoves[1]、makeSelfMoves[2]、makeSelfMoves[3]); 打印件(codeFile.PieceState()); //MessageBox.Show(“行开始:+makeSelfMoves[0]+”。行结束:+makeSelfMoves[2]+”。列开始:+makeSelfMoves[1]+。列结束:+makeSelfMoves[3]+”。a是:+a); }_C#_Winforms_Timer_Iteration - Fatal编程技术网

C# winform中带有迭代的计时器 foreach(重播中的KeyValuePair项)//计数应大于2 { makeSelfMoves=replay[item.Key]; ExecuteAll(makeSelfMoves[0]、makeSelfMoves[1]、makeSelfMoves[2]、makeSelfMoves[3]); 打印件(codeFile.PieceState()); //MessageBox.Show(“行开始:+makeSelfMoves[0]+”。行结束:+makeSelfMoves[2]+”。列开始:+makeSelfMoves[1]+。列结束:+makeSelfMoves[3]+”。a是:+a); }

C# winform中带有迭代的计时器 foreach(重播中的KeyValuePair项)//计数应大于2 { makeSelfMoves=replay[item.Key]; ExecuteAll(makeSelfMoves[0]、makeSelfMoves[1]、makeSelfMoves[2]、makeSelfMoves[3]); 打印件(codeFile.PieceState()); //MessageBox.Show(“行开始:+makeSelfMoves[0]+”。行结束:+makeSelfMoves[2]+”。列开始:+makeSelfMoves[1]+。列结束:+makeSelfMoves[3]+”。a是:+a); },c#,winforms,timer,iteration,C#,Winforms,Timer,Iteration,我想在1秒的时间间隔内执行整个迭代,我负责游戏重播。我在表格中放置了一个计时器,并将其设置为1秒(这将使碎片以1秒的间隔移动)。我创建了一个事件,并在循环之前放入语句timer1.enabled=true。迭代将以快速的方式结束。。如何使用计时器设置迭代以使用计时器每秒执行 public void ReplayGame() { Class2.Replayson=true; replay=serializeMeh.giveBackDictionary(); int[]makeSelfMoves=

我想在1秒的时间间隔内执行整个迭代,我负责游戏重播。我在表格中放置了一个计时器,并将其设置为1秒(这将使碎片以1秒的间隔移动)。我创建了一个事件,并在循环之前放入语句timer1.enabled=true。迭代将以快速的方式结束。。如何使用计时器设置迭代以使用计时器每秒执行


public void ReplayGame()
{
Class2.Replayson=true;
replay=serializeMeh.giveBackDictionary();
int[]makeSelfMoves=新int[4];
//定时器t=新定时器();
//t、 间隔=1000;
//t、 滴答声+=计时器1\u滴答声;
//t、 启用=真;
//t、 Start();
if(backgroundWorker1.IsBusy!=true)
{
//启动异步操作。
backgroundWorker1.RunWorkerAsync();
}
//foreach(replay中的KeyValuePair项)//计数应大于2
//{
//makeSelfMoves=replay[item.Key];
//ExecuteAll(makeSelfMoves[0]、makeSelfMoves[1]、makeSelfMoves[2]、makeSelfMoves[3]);
//打印件(codeFile.PieceState());
//显示(“行开始:+makeSelfMoves[0]+”。行结束:+makeSelfMoves[2]+”。列开始:+makeSelfMoves[1]+”。列结束:+makeSelfMoves[3]);
//}
}
如果我想要重播,则激活上述方法

  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\u DoWork(对象发送方,DoWorkEventArgs e)
{
BackgroundWorker worker=发件人作为BackgroundWorker;
int[]makeSelfMoves=新int[4];
foreach(replay中的KeyValuePair项)//计数应大于2
{
makeSelfMoves=replay[item.Key];//提供单个移动位置的数组startrow、startcolumn、endrow、endcolun
ExecuteAll(makeSelfMoves[0]、makeSelfMoves[1]、makeSelfMoves[2]、makeSelfMoves[3]);
打印件(codeFile.PieceState());//在电路板上打印代码
系统线程线程睡眠(1000);
}
}

它确实起作用了,但我遇到的问题是,循环结束后,存在的碎片消失了。是不是因为背景工人,因为没有背景工人,我就不会在过程的最后消失。第二次激活该方法时,它会在后台线程中运行,并将thread.Sleep(1000)放入循环中


这样,它将基于时间,而不是冻结您的应用程序。

我们需要查看您的更多代码,但非常简单:

使用System.Windows.Forms

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

    }
SomeMethod(…)
{
定时器t=新定时器();
t、 间隔=1000;
t、 滴答声+=滴答声;
t、 启用=真;
t、 Start();
}
无效t_勾选(…)
{
foreach(replay中的KeyValuePair项)//计数应大于2
{           
makeSelfMoves=replay[item.Key];
ExecuteAll(makeSelfMoves[0]、makeSelfMoves[1]、makeSelfMoves[2]、makeSelfMoves[3]);
打印件(codeFile.PieceState());
//MessageBox.Show(“行开始:+makeSelfMoves[0]+”。行结束:+makeSelfMoves[2]+”。列开始:+makeSelfMoves[1]+。列结束:+makeSelfMoves[3]+”。a是:+a);
}
}

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


如果你坚持使用计时器,我建议你使用“Ed”的建议。当然,计时器也可以放在表单上,而不是在代码中创建它。只是你喜欢什么。

不清楚你想做什么。请多写点。我认识背景工作者,我想我可以用定时器来完成。我在表单上放置了计时器,但它的行为异常,请参见下面的代码wi get invalidoOperationException,对象当前在其他位置使用。异常位于主静态void main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(新棋盘());//就在这里}
        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);
        }

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