初学多线程时遇到的一个小问题 关于System.Threading.Timer 和TimerCallback
winForm 上有2个textBox 控件:textBox1和textBox2, 一个button控件,想实践一下System.Threading.Timer的功能。我用一个线程执行一个循环,给一个变量赋值,然后想每隔1秒钟去检查这个变量如果大于某个值就将textBox2的背景色改变,我用如下方法总是没有实现,textBox2的颜色没有发生改变。所以想请大家看看问题出在哪里,谢谢!
---------------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
private int nTemp=0;//定义了一个整型变量
-------------------------------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
Thread t=new Thread(new ThreadStart(fun1));
t.Start();
TimerCallback tmrCallBack=new TimerCallback(fun2);
System.Threading.Timer tmr=new System.Threading.Timer(tmrCallBack,null,TimeSpan.Zero,TimeSpan.FromSeconds(2));
}
----------------------------------------------------------------------------------
private void fun1()
{
int i;
for (i=0;i<10000;i++)
{
nTemp=i;
textBox1.Text=i.ToString();
}
}
_________________________________________________________________________________
private void fun2()
{
if (nTemp<100)
{
textBox2.BackColor=System.Drawing.Color.Blue;
}
else if (nTemp>100&&nTemp<1000)
{
textBox2.BackColor=System.Drawing.Color.Green;
}
else if (nTemp>1000&&nTemp<10000)
{
textBox2.BackColor=System.Drawing.Color.Yellow;
}
}
_________________________________________________________________________________
问题点数:50、回复次数:3Top
1 楼amandag(高歌)回复于 2006-06-04 19:38:15 得分 50
应该这样去声明:
private int nTemp=0;//定义了一个整型变量
private System.Threading.Timer tmr;
-------------------------------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
Thread t=new Thread(new ThreadStart(fun1));
t.Start();
TimerCallback tmrCallBack=new TimerCallback(fun2);
tmr=new System.Threading.Timer(tmrCallBack,null,TimeSpan.Zero,TimeSpan.FromSeconds(2));
}
----------------------------------------------------------------------------------
帮助中说的很清楚:只要在使用 Timer,就必须保留对它的引用。对于任何托管对象,如果没有对 Timer 的引用,计时器会被垃圾回收。即使 Timer 仍处在活动状态,也会被回收。也就是说当button1_Click()执行结束的时候由于tmr出了声明周期而导致new System.Threading.Timer(tmrCallBack,null,TimeSpan.Zero,TimeSpan.FromSeconds(2))被回收。Top
2 楼amandag(高歌)回复于 2006-06-04 19:38:42 得分 0
//完整的代码如下
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace WindowsThread
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
private System.Threading.Timer tmr;
private int nTemp=0;//定义了一个整型变量
/// <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.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(8, 8);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(120, 8);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(248, 93);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Thread t=new Thread(new ThreadStart(fun1));
t.Start();
TimerCallback tmrCallBack=new TimerCallback(fun2);
tmr=new System.Threading.Timer(tmrCallBack,null,TimeSpan.Zero,TimeSpan.FromSeconds(2));
}
private void fun1()
{
int i;
for (i=0;i<30000;i++)
{
nTemp=i;
textBox1.Text=i.ToString();
}
}
private void fun2(object state)
{
if (nTemp<10000)
{
textBox2.BackColor=System.Drawing.Color.Blue;
}
else if (nTemp<20000)
{
textBox2.BackColor=System.Drawing.Color.Green;
}
else if (nTemp<30000)
{
textBox2.BackColor=System.Drawing.Color.Yellow;
}
}
}
}
Top
3 楼akinggmx(为了生活而编程)回复于 2006-06-04 20:36:41 得分 0
谢谢受益匪浅Top




