关于一个winforms的程序问题

chinaliudan 2006-10-29 11:56:06
就是我要做一个winforms的程序,在界面的顶端会往下落图片,当图片落到窗体的底端以外就消除.我是用Timer控件控制每阁1秒就NEW一个PictureBox,并且创建一个线程控制这个图片往下走.然后判断图片超过窗体底端就用Controls.Remove();的方法将控件从窗体上祛除,然后用Dispose()的方法消除.但是只要超过10个图片超过底端时,就狂吃计算机资源,卡,卡到一定程度就没响应了.是怎么引起的啊?
具体代码如下:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace WindowsApplication1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private Thread thread1;
private Thread thread2;
private Thread thread3;
private Thread thread4;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Timer timer2;
private System.ComponentModel.IContainer components;

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.components = new System.ComponentModel.Container();
this.label1 = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(144, 208);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 64);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 317);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);

}
#endregion

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


private void TopMove()
{
if (this.label1.Location.Y > 0)
{
this.label1.Location = new Point(this.label1.Location.X,this.label1.Location.Y - 2);
}
}

private void DownMove()
{
if (this.label1.Location.Y + this.label1.Height < this.Height)
{
this.label1.Location = new Point(this.label1.Location.X,this.label1.Location.Y + 2);
}
}

private void LeftMove()
{
if (this.label1.Location.X > 0)
{
this.label1.Location = new Point(this.label1.Location.X - 2,this.label1.Location.Y);
}
}

private void RightMove()
{
if (this.label1.Location.X + this.label1.Width < this.Width)
{
this.label1.Location = new Point(this.label1.Location.X + 2,this.label1.Location.Y);
}
}

private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode.ToString() == "Up")
{
this.thread1 = new Thread(new ThreadStart(this.TopMove));
this.thread1.Start();
}
if (e.KeyCode.ToString() == "Down")
{
this.thread2 = new Thread(new ThreadStart(this.DownMove));
this.thread2.Start();
}
if (e.KeyCode.ToString() == "Right")
{
this.thread3 = new Thread(new ThreadStart(this.RightMove));
this.thread3.Start();
}
if (e.KeyCode.ToString() == "Left")
{
this.thread4 = new Thread(new ThreadStart(this.LeftMove));
this.thread4.Start();
}
}

private void timer1_Tick(object sender, System.EventArgs e)
{
Random rnd = new Random();
PictureBox pb = new PictureBox();
pb.Image= Image.FromFile("1.gif");
pb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.Controls.Add(pb);
pb.Location = new Point(rnd.Next(2,this.Width - pb.Width));
Move m = new Move(this,pb);
Thread thread = new Thread(new ThreadStart(m.Down));
thread.Start();
}





}

public class Move
{
Form form;
PictureBox pb;

public Move(Form form1,PictureBox pb1)
{
form = form1;
pb = pb1;
}

public void Down()
{
Random rnd = new Random();

while (true)
{
if (pb.Top <= form.Height)
{
pb.Location = new Point(pb.Location.X + (rnd.Next(-2,2)),pb.Top + 2);
Thread.Sleep(100);
}
else
{
if (pb == null)
{
break;
}
form.Controls.Remove(pb);
pb.Dispose();
}

}



}
}
}
...全文
78 1 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
lovvver 2006-10-29
  • 打赏
  • 举报
回复
为什么要“每阁1秒就NEW一个PictureBox”呢?
你不可以改变它的显示位置吗?

你用picturebox来实现你要的效果,感觉不太妥当,建议看看GDI。
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2006-10-29 11:56
社区公告

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