一个按钮乱跑!停不下来!程序无法关闭

weixin_43936063 2019-04-18 10:28:33


园代码如下:
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);
}
}

}
}
}

程序无法关闭
...全文
425 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
ilovekworm 2019-04-25
  • 打赏
  • 举报
回复
按钮事件放循环内了,无法正常响应你的关闭操作。放主线程试试。
程序员的键盘 2019-04-25
  • 打赏
  • 举报
回复
大概等14分钟就可以停下来了 不要太过担心
(339*7+268*7)*2*100/1000/60=14
bbjiabcd 2019-04-25
  • 打赏
  • 举报
回复
别用Thread.Sleep(); 用Timer写,改为异步执行
龍月 2019-04-24
  • 打赏
  • 举报
回复
任务管理器 找到程序进程 kill
平底锅锅锅 2019-04-23
  • 打赏
  • 举报
回复
有可能死循环了。
Nekoxy 2019-04-23
  • 打赏
  • 举报
回复

//按钮的事件改成
Thread th = new Thread(() =>
            {
            //此处执行你的循环代码
            for(;;)
            {
                        //所有对按钮之类的控件的操作
                        Invoke(new Action(() =>{    button1.Location = new Point(x, y);             }));
            }); 
            }
           
            th.Start();
weixin_43936063 2019-04-19
  • 打赏
  • 举报
回复
还是不明白各位说的细点么
xian_wwq 2019-04-18
  • 打赏
  • 举报
回复
自己定义个线程,把功能代码放进去
当然线程中不能直接与UI交互,需要使用委托
自行搜 “跨线程访问”
weixin_43936063 2019-04-18
  • 打赏
  • 举报
回复
怎么不放入主线程 又能正常关闭
weixin_43936063 2019-04-18
  • 打赏
  • 举报
回复

两处无法关闭
xian_wwq 2019-04-18
  • 打赏
  • 举报
回复
for (int i = 0; i < 100; i++)
都写了循环了,当然是得把循环执行完

而且代码放在主线程中,
肯定是无法响应用户操作,类似于失控

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧