| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 906 人关注过本帖
标题:[求助]紧急求助while循环问题
只看楼主 加入收藏
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
结帖率:100%
收藏
 问题点数:0 回复次数:9 
[求助]紧急求助while循环问题

各位大侠帮我看看下面的代码,我写的一个画圆点程序,在写while循环时出现错误了,当我运行时一点击窗口,它就说“程序未响应”要强行关闭,谢谢各位!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace waTenCharacter
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Button btnClose;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.btnClose = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(192, 48);
this.btnStart.Name = "btnStart";
this.btnStart.TabIndex = 0;
this.btnStart.Text = "Start";
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(192, 96);
this.btnStop.Name = "btnStop";
this.btnStop.TabIndex = 1;
this.btnStop.Text = "Stop";
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// btnClose
//
this.btnClose.Location = new System.Drawing.Point(192, 144);
this.btnClose.Name = "btnClose";
this.btnClose.TabIndex = 0;
this.btnClose.Text = "Close";
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnStart);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void btnStart_Click(object sender, System.EventArgs e)
{
int width = 10;
int high = 10;
int n = 1;

Graphics g = this.CreateGraphics();
System.Drawing.Drawing2D.LinearGradientBrush myBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,Color.Gray, Color.Gray, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
System.Drawing.Drawing2D.LinearGradientBrush myBrush1 = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,Color.Green, Color.Green, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
System.Drawing.Drawing2D.LinearGradientBrush myBrush2 = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle,Color.Red, Color.Red, System.Drawing.Drawing2D.LinearGradientMode.Vertical);

//g.FillRectangle(myBrush,x,y,width,high);
while(n<n+1)
{
n++;
for(int y = 0;y<100;y=y+10)
{
for(int x = 0; x<100;x= x+10)
{
g.FillEllipse(myBrush,x,y,width,high);
if(x == 50)
{
g.FillEllipse(myBrush1,50,y,width,high);
//System.Threading.Thread.Sleep(10);
}
else if(x == 40)
{
g.FillEllipse(myBrush1,40,y,width,high);
}
else if(y == 50)
{
g.FillEllipse(myBrush1,x,50,width,high);
}
else if(y == 40)
{
g.FillEllipse(myBrush1,x,40,width,high);
}
//System.Threading.Thread.Sleep(10);
}
}
System.Threading.Thread.Sleep(1000);
for(int y = 0;y<100;y=y+10)
{
for(int x = 0; x<100;x= x+10)
{
g.FillEllipse(myBrush,x,y,width,high);
if(x == 50)
{
g.FillEllipse(myBrush2,50,y,width,high);
//System.Threading.Thread.Sleep(10);
}
else if(x == 40)
{
g.FillEllipse(myBrush2,40,y,width,high);
}
else if(y == 50)
{
g.FillEllipse(myBrush2,x,50,width,high);
}
else if(y == 40)
{
g.FillEllipse(myBrush2,x,40,width,high);
}
//System.Threading.Thread.Sleep(10);
}
}
System.Threading.Thread.Sleep(1000);
//continue;
}


}

private void btnStop_Click(object sender, System.EventArgs e)
{

}

private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
}
}

搜索更多相关主题的帖子: using System summary Forms 
2006-08-21 11:37
mylover624
Rank: 1
来 自:乖乖的心中
等 级:新手上路
帖 子:868
专家分:0
注 册:2006-7-6
收藏
得分:0 
你这不是个死循环吗?

一个天才顶不上十个笨蛋!
书山有路勤为径,学海无涯友相伴。
我的E-mail:mylover624@.cn
2006-08-21 11:52
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 

我就是想让他一直循环啊,但是不知道为什么不行哦,还请你指点指点,只要反复执行while之间的语句就行,多谢!


汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2006-08-21 13:26
mylover624
Rank: 1
来 自:乖乖的心中
等 级:新手上路
帖 子:868
专家分:0
注 册:2006-7-6
收藏
得分:0 
我试过了,如果把 while(n<n+1) 中的"n+1"快捷方式成一个数字,就没的问题了.
这样一直循环,就不能做其它的操作了啊.

一个天才顶不上十个笨蛋!
书山有路勤为径,学海无涯友相伴。
我的E-mail:mylover624@.cn
2006-08-21 13:32
tanghuawei
Rank: 4
来 自:美丽的湖南
等 级:业余侠客
威 望:3
帖 子:531
专家分:220
注 册:2006-3-16
收藏
得分:0 

有没有其他的办法?我可能要在这里写很多效果,我做的是一个十字LED屏的编播软件,谢谢!


汽车尾气检测网络系统QQ:357766186__MSN:MSNTHW19850316@
2006-08-21 13:38
小海龟
Rank: 6Rank: 6
等 级:贵宾
威 望:23
帖 子:1068
专家分:4
注 册:2006-8-1
收藏
得分:0 

用一个线程控制啊,或者用一个timer控件去控制循环啊。


[bc09] 犯强汉者,虽远比诛!
2006-08-21 15:54
mylover624
Rank: 1
来 自:乖乖的心中
等 级:新手上路
帖 子:868
专家分:0
注 册:2006-7-6
收藏
得分:0 
在这里用Timer好像不好吧

一个天才顶不上十个笨蛋!
书山有路勤为径,学海无涯友相伴。
我的E-mail:mylover624@.cn
2006-08-21 16:56
小熙
Rank: 1
等 级:等待验证会员
帖 子:36
专家分:0
注 册:2006-5-11
收藏
得分:0 
没有任何一个功能必须要用死循环来实现?肯定还有其他实现的办法.
2006-08-21 17:02
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
收藏
得分:0 
以下是引用小熙在2006-8-21 17:02:13的发言:
没有任何一个功能必须要用死循环来实现?肯定还有其他实现的办法.

好多功能必須使用死循環


[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-08-21 17:58
月夜枫华
Rank: 4
等 级:贵宾
威 望:12
帖 子:437
专家分:42
注 册:2006-1-2
收藏
得分:0 
使用死循环可以,但是最好不要在主程上执行死循环,如果循环中没有Thread.Sleep()则程序将占尽系统资源,如果有则在线程睡眠时间内,窗体无法响应用户输入.
解决办法:将你的画图代码放入线程或线程池内执行!

2006-08-21 19:28
快速回复:[求助]紧急求助while循环问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016134 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved