time_tick控件中两个for循环只执行一个

.Net技术 码拜 8年前 (2016-06-08) 976次浏览
尝试改写了一个书上循环改变label背景颜色的程序。
但是这个程序运行后,一开始两个for循环只执行一个,点击停止后,会开始运行第二个for循环,再点击才会停止,而假如只写一个for循环,就能正常停止time控件,搞不清楚为什么会这样?time_tick不是会循环执行其内全部程序吗?
time.interval=1000【一开始设定为100,程序跑的飞速,结果电脑卡机了..interval一般应该设定为多少为好?】
在控制label背景颜色变换的书上原来的程序,使用的是while循环,结果导致点击两次,窗口才能关闭,而这个更改的程序,必须等待time当前一次运行结束,假如本人想点击结束,立刻结束程序,停留在当前颜色,问一下应该怎么做?
更改的程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace solution
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if(checkBox1.Checked!=true)
            {
                MessageBox.Show("Wrong");
            }
            else
            {
               
                timer1.Start();
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            for(int c=0;c<255;c++)
            {
                label1.BackColor = Color.FromArgb(c, 255 - c, c);
                Application.DoEvents();
                System.Threading.Thread.Sleep(3);
            }
            for (int c = 255; c >= 0; c--)
            {
                label1.BackColor = Color.FromArgb(c, 255 - c, c);
                Application.DoEvents();
                System.Threading.Thread.Sleep(3);
            }
         
        }
        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            timer1.Stop();
        }
       
    }
}

书上原来提供的程序:
[code=csharp][/using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
bool bFlag = false;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked != true)//验证checkbox能否被选中
MessageBox.Show(“the box is not checked”);
else
{
while (Visible)                {
for (int c = 0; c < 255 && Visible == true; c++)//循环一遍颜色
{
label1.BackColor = Color.FromArgb(c, 255 – c, c);
Application.DoEvents();//让程序暂时停止循环。
System.Threading.Thread.Sleep(3);
}
for (int c = 0; c < 255 ; c++)
{
label1.BackColor = Color.FromArgb(c, 255 – c, c);
Application.DoEvents();//让程序暂时停止循环。
System.Threading.Thread.Sleep(3);
}

{
label1.BackColor = Color.FromArgb(c, 255 – c, c);
Application.DoEvents();
System.Threading.Thread.Sleep(3);
}
}
}
}
}
}code]

解决方案

60

逻辑本身是很简单的。假如你每天发一份快递,一个月发几十份快递,你会让快递员呆在你门口吃喝拉撒睡都不离开吗?
设计“定时做一个事情”,跟一个刚学编程语法的学生在课堂上学编程时学的循环概念,是两个完全不同的概念。
这种程序根本不写循环语句。最垃圾的程序就是动不动就写一个大循环语句,是设计概念问题。

20

引用:

n的初始值是1,control的初始值是0.

   private void timer1_Tick(object sender, EventArgs e)
        {
            control++;
            if (n%2==1)
            {
                label1.BackColor = Color.FromArgb(c, 255 - c, c);
                c++;
     
            }
            if (control % 255 == 0)
            {
                n++;
            }
            if (n%2==0)
            {
                  label1.BackColor = Color.FromArgb(c, 255 - c, c);
                  c--;
                
            }
        }

当然可以,不过代码可以精简一点

private void timer1_Tick(object sender, EventArgs e)
{
    c += (n % 2 == 0 ? -1 : 1);
    label1.BackColor = Color.FromArgb(c, 255 - c, c);
    if (++control % 255 == 0) n++;
    // label1.Text = label1.BackColor.ToString();
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明time_tick控件中两个for循环只执行一个
喜欢 (0)
[1034331897@qq.com]
分享 (0)