每2秒更新一次WPF GUI(C#)

时间:2014-04-29 16:46:28

标签: c# wpf user-interface thread-sleep 8-puzzle

我正在做一个8 Puzzle解算器,最终将每个节点(元素0-8的int [])存储在路径中,以便将块按顺序放入堆栈中。我有一个显示int [,]

的WPF GUI
foreach (var node in stack)
        {
            int[,] unstrung = node.unstringNode(node); // turns node of int[] into board of  int[,]
            blocks.setBoard(unstrung); // sets the board to pass in to the GUI
            DrawBoard(); // Takes the board (int[,]) and sets the squares on the GUI to match it.
            Thread.Sleep(500);
        }

GUI显示初始板,然后在我单击“解决”后,正确显示最终(按顺序)板。我想要做的是在电路板上显示每个节点一段时间,最终到达有序电路板。使用Thread.Sleep,GUI将在显示最终节点之前暂停一段时间。关于为什么这段代码不会每隔500毫秒显示每个节点的电路板的任何想法?

供参考,以下是Console.Write为节点输出的示例:
4,2,3,6,1,0,7,5,8
4,2,0,6,1,3,7,5,8
4,0,2,6,1,3,7,5,8
4,1,2,6,0,3,7,5,8
4,1,2,0,6,3,7,5,8
0,1,2,4,6,3,7,5,8
1,0,2,4,6,3,7,5,8
1,2,0,4,6,3,7,5,8
1,2,3,4,6,0,7,5,8
1,2,3,4,0,6,7,5,8
1,2,3,4,5,6,7,0,8
1,2,3,4,5,6,7,8,0

1 个答案:

答案 0 :(得分:-1)

编辑:

由于我的原始答案是使用Thread代替Timer而被低估的,因此以下是使用计时器的示例。

使用Thread的代码更短,我想快速给他一个解决方案。

此外,使用Thread代替计时器意味着他不需要以不同方式传递参数或重组他的循环。

这就是为什么讨论替代解决方案的优缺点而不是简单地坚持只有一种正确的方法是个好主意。

使用timer_Tick功能更新位置。

您可能会注意到这会使原始代码变得复杂,因为您必须以不同方式传递参数并重新构建循环。

public partial class Form1 : Form
{
    private Point pos = new Point(1,1);
    private float[] vel = new float[2];
    private Size bounds = new Size(20,20);
    private Timer ticky = new Timer();      //System.Windows.Forms.Timer
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        ticky.Interval = 20;
        ticky.Tick += ticky_Tick;
        vel[0] = 4; vel[1] = 0;
        ticky.Start();
    }
    void ticky_Tick(object sender, EventArgs e)
    {
        updatePosition();
        //This tells our form to repaint itself (and call the OnPaint method)
        this.Invalidate();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        e.Graphics.FillEllipse(new SolidBrush(Color.LightBlue), new Rectangle(pos, bounds));
    }
    private void updatePosition()
    {
        pos = new Point(pos.X + (int)vel[0], pos.Y + (int)vel[1]);
        vel[1] += .5f; //Apply some gravity
        if (pos.X + bounds.Width > this.ClientSize.Width)
        {
            vel[0] *= -1;
            pos.X = this.ClientSize.Width - bounds.Width;
        }
        else if (pos.X < 0)
        {
            vel[0] *= -1;
            pos.X = 0;
        }
        if (pos.Y + bounds.Height > this.ClientSize.Height)
        {
            vel[1] *= -.90f; //Lose some velocity when bouncing off the ground
            pos.Y = this.ClientSize.Height - bounds.Height;
        }
        else if (pos.Y < 0)
        {
            vel[1] *= -1;
            pos.Y = 0;
        }
    }
}

结果:

Results

您可以使用计时器进行各种延迟表格绘制:

More

原始解决方案:

//Create a separate thread so that the GUI thread doesn't sleep through updates:
using System.Threading;

new Thread(() => {
    foreach (var node in stack)
    {
        //The invoke only needs to be used when updating GUI Elements
        this.Invoke((MethodInvoker)delegate() {
            //Everything inside of this Invoke runs on the GUI Thread
            int[,] unstrung = node.unstringNode(node); // turns node of int[] into board of  int[,]
            blocks.setBoard(unstrung); // sets the board to pass in to the GUI
            DrawBoard(); // Takes the board (int[,]) and sets the squares on the GUI to match it.
        });
        Thread.Sleep(500);
    }
}).Start();