在整个循环完成之前

在整个循环完成之前

本文介绍了在整个循环完成之前,标签文本不会更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Winform程序,当用户点击一个按钮然后调用picturebox paint事件来根据计算结果绘制一个新的BMP时进行一些计算。这很好。

I have a Winform program that does some calculations when the user clicks on a button and then calls the picturebox paint event to draw a new BMP based on the results of the calculations. This works fine.

现在我想这样做100次,每次刷新图片框时,我想通过更新文本来查看它当前所处的迭代一个标签如下:

Now I want to do this 100 times and every time the picturebox is refreshed, I want to see the iteration that it's currently in by updating the text on a label as per below:

 private void button2_Click(object sender, EventArgs e)
        {

        for (int iterations = 1; iterations <= 100; iterations++)
        {
            // do some calculations to change the cellmap parameters
            cellMap.Calculate();

            // Refresh picturebox1
            pictureBox1.Invalidate();
            pictureBox1.Update();

            // Update label with the current iteration number
            label1.Text = iterations.ToString();
        }
    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {

        Bitmap bmp = new Bitmap(cellMap.Dimensions.Width, cellMap.Dimensions.Height);
        Graphics gBmp = Graphics.FromImage(bmp);

        int rectWidth = scaleFactor;
        int rectHeight = scaleFactor;

         // Create solid brushes
        Brush blueBrush = new SolidBrush(Color.Blue);
        Brush greenBrush = new SolidBrush(Color.Green);
        Brush transparentBrush = new SolidBrush(Color.Transparent);

        Graphics g = e.Graphics;

        for (int i = 0; i < cellMap.Dimensions.Width; i++)
        {
                for (int j = 0; j < cellMap.Dimensions.Height; j++)
                {
                    // retrieve the rectangle and draw it
                    Brush whichBrush;

                    if (cellMap.GetCell(i, j).CurrentState == CellState.State1)
                    {
                        whichBrush = blueBrush;
                    }
                    else if (cellMap.GetCell(i, j).CurrentState == CellState.State2)
                    {
                        whichBrush = greenBrush;
                    }
                    else
                    {
                        whichBrush = transparentBrush;
                    }

                    // draw rectangle to bmp
                    gBmp.FillRectangle(whichBrush, i, j, 1f, 1f);
                }
         }

         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
         g.DrawImage(bmp, 0, 0, pictureBox1.Width, pictureBox1.Height);
    }

我遇到的问题是标签文本只显示在最后一个之后图片框更新完成。基本上,它不显示1到99.我可以看到每次刷新后的图片框更新,因为BMP随着每次迭代而变化。有什么想法吗?

The problem I am having is that the label text only gets displayed after the last picturebox update is completed. So essentially, it does not display 1 through to 99. I can see the picturebox updates after every refresh as the BMP changes with every iteration. Any idea?

推荐答案

回答你为什么要这样做的问题:Windows Forms程序在一个线程中运行所有东西 - UI线程。这意味着它必须按顺序执行代码,以便在它切换回UI代码之前完成一个函数。换句话说,它在完成功能之前无法更新图片,因此如果您更新图片100次,则只会更新最后一张图片。使用Invalidate / Update代码告诉编译器暂停函数的执行并强制它更新UI而不是等到函数结束。希望有所帮助!

To answer your question about why you have to do it: Windows Forms Programs run everything in a single thread - the UI thread. This means it must execute code in order, so that it will finish a function before it can switch back to the UI code. In other words, it can't update the pictures until after its finished with the function, so if you updated the picture 100 times, only the last one will actually get updated. Using the Invalidate/Update code tells the compiler to "pause" execution of the function and forces it to update the UI instead of waiting till the end of the function. Hope that helps!

这篇关于在整个循环完成之前,标签文本不会更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:44