本文介绍了在结束之前嵌套for循环停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行我的代码时,有时会对表单进行绘制,有时它会卡住。

所以我在调试模式下运行它,我发现第一个循环不循环!

循环总共运行65025次,因此它与我的电脑无关。



When I run my code, sometimes the form will be painted and sometimes it will just get stuck.
So I ran it in debug mode and I found that the first loop doesn't loop!
The loops ran 65025 total number of times so it has nothing to do with my PC.

//frm is System.Windows.Forms.Form
for (byte z = 0; z < 255; z++)
            {
                for (byte x = 0; x < 255; x++)
                {
                    for (byte y = 0; y < 255; y++)
                    {
                        Color c = Color.FromArgb(z, x, y);
                        frm.CreateGraphics().FillRectangle(new SolidBrush(c), x, y, 1,    1);
                        frm.Refresh();
                        frm.Show();
                    }
                }
            }



有什么想法吗?


Any ideas?

推荐答案


using (Graphics gr = this.CreateGraphics())
{

    for (byte z = 128; z < 255; z++)
    {
        for (byte x = 0; x < 255; x++)
        {
            for (byte y = 0; y < 255; y++)
            {
                Color c = Color.FromArgb(z, x, y);

                gr.FillRectangle(new SolidBrush(c), x, y, 1, 1);
            }
        }

       this.Refresh();
    }
}


这篇关于在结束之前嵌套for循环停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 07:36