如何让timer 停止?

xiangdafang 2006-08-07 01:36:14
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace x3
{
public partial class Form1 : Form

{


public static void Main()
{
Application.Run(new Form1());


}

public Form1()
{
//InitializeComponent();
Text = "hello:";
Timer timer=new Timer() ;
timer.Interval =10;

timer.Tick +=new EventHandler (TimerOnTick);
timer.Start();


}
void TimerOnTick(object obj,EventArgs ea)
{
Random rand= new Random ();
int x1=rand.Next(ClientSize.Width +100);
int x2=rand.Next(ClientSize.Width -100);
int y1=rand.Next(ClientSize.Height-200 );
int y2=rand.Next(ClientSize.Height-200);
Color color =Color.FromArgb(rand.Next(256),
rand.Next(256),
rand.Next(256));
Graphics grfx=CreateGraphics ();
grfx.FillRectangle (new SolidBrush (color),Math .Min (x1,x2),Math.Min(y1,y2),
Math.Abs(x2-x1),Math.Abs(y2-y1));
grfx.Dispose ();

}






}
}


如何让timer timer 停止运行,
...全文
1166 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
www_123du_com 2006-08-07
  • 打赏
  • 举报
回复
System.Windows.Forms.Timer timer1;
timer1.Stop();

System.Timers.Timer timer2;
timer2.Stop();

System.Threading.Timer timer3;
timer3.Change(-1,-1); //不知道是不是必需
timer3.Dispose();
shicj 2006-08-07
  • 打赏
  • 举报
回复
timer.stop();
timer.enable = false;
jackymi 2006-08-07
  • 打赏
  • 举报
回复
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label label1;
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button btnStop;
Timer timer;
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.btnStop = new System.Windows.Forms.Button();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(112, 136);
this.btnStop.Name = "btnStop";
this.btnStop.TabIndex = 0;
this.btnStop.Text = "结束";
this.btnStop.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(112, 72);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnStop);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

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

private void Form1_Load(object sender, System.EventArgs e)
{
Text = "hello:";
timer=new Timer() ;
timer.Interval =10;

timer.Tick +=new EventHandler (TimerOnTick);
timer.Start();

}
void TimerOnTick(object obj,EventArgs ea)
{
Random rand= new Random ();
int x1=rand.Next(ClientSize.Width +100);
int x2=rand.Next(ClientSize.Width -100);
int y1=rand.Next(ClientSize.Height-200 );
int y2=rand.Next(ClientSize.Height-200);
Color color =Color.FromArgb(rand.Next(256),
rand.Next(256),
rand.Next(256));
Graphics grfx=CreateGraphics ();
grfx.FillRectangle (new SolidBrush (color),Math .Min (x1,x2),Math.Min(y1,y2),
Math.Abs(x2-x1),Math.Abs(y2-y1));
grfx.Dispose ();

}

private void button1_Click(object sender, System.EventArgs e)
{
timer.Stop();
}



}
}

110,545

社区成员

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

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

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