园代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;namespace SJ {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e) {
            int x, y;
            for (int i = 0; i < 100; i++) {
                x = 1;
                y = 1;
                while (x < 339) {
                    button1.Location = new Point(x, y);
                    x++;
                    Thread.Sleep(7);
                }
                while (y < 268) {
                    button1.Location = new Point(x, y);
                    y++;
                    Thread.Sleep(7);
                }
                while (x > 1) {
                    button1.Location = new Point(x, y);
                    x--;
                    Thread.Sleep(7);
                }
                while (y > 1) {
                    button1.Location = new Point(x, y);
                    y--;
                    Thread.Sleep(7);
                }
            }        }
    }
}程序无法关闭

解决方案 »

  1.   

    for (int i = 0; i < 100; i++) 
    都写了循环了,当然是得把循环执行完而且代码放在主线程中,
    肯定是无法响应用户操作,类似于失控
      

  2.   


    //按钮的事件改成
    Thread th = new Thread(() =>
                {
                //此处执行你的循环代码
                for(;;)
                {
                            //所有对按钮之类的控件的操作
                            Invoke(new Action(() =>{    button1.Location = new Point(x, y);             }));
                }); 
                }
               
                th.Start();
      

  3.   

    任务管理器
    找到程序进程
    kill
      

  4.   

    别用Thread.Sleep(); 用Timer写,改为异步执行