各位大虾:
Form1里有两个Button一个label1,点button1后启动另一个线程来生成一个Class2的对像,Class2中有个timer每隔500ms使i加一,并通过Form1的Label显示出来;点button2后不启动另一个线程,直接生成一个Class2的对像。为什么点button1不管用也不提示错误,Form1中的Label未变化。而点button2后正常??????????
代码如下//---------------------Form1-------------------------using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
public static System.Windows.Forms.Label label_S;
private System.Windows.Forms.Button button2;
private System.ComponentModel.IContainer components;
public Form1()
{
InitializeComponent();
Form1.label_S=this.label1;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(72, 104);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(208, 32);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public static void TheadS()
{
Class1 c1=new Class1();
c1.Start();
}
private void button1_Click(object sender, System.EventArgs e)
{
Thread td=new Thread(new ThreadStart(TheadS));//创建另一个线程启动Label并无变化。
td.Start();
} private void button2_Click(object sender, System.EventArgs e)
{
TheadS();// 直接启动显示正常。
}
}
}//--------------------Class1---------------------
using System;
using System.Windows.Forms;
namespace WindowsApplication2
{
public class Class1
{
private System.Windows.Forms.Timer timer1=new Timer();
private int i=0;
public Class1()
{
this.timer1.Tick+=new EventHandler(timer1_Tick);
this.timer1.Interval=500; }
public void Start()
{
this.timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
Form1.label_S.Text=(i++).ToString();
}
}
}
Form1里有两个Button一个label1,点button1后启动另一个线程来生成一个Class2的对像,Class2中有个timer每隔500ms使i加一,并通过Form1的Label显示出来;点button2后不启动另一个线程,直接生成一个Class2的对像。为什么点button1不管用也不提示错误,Form1中的Label未变化。而点button2后正常??????????
代码如下//---------------------Form1-------------------------using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
public static System.Windows.Forms.Label label_S;
private System.Windows.Forms.Button button2;
private System.ComponentModel.IContainer components;
public Form1()
{
InitializeComponent();
Form1.label_S=this.label1;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(72, 104);
this.label1.Name = "label1";
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(208, 32);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public static void TheadS()
{
Class1 c1=new Class1();
c1.Start();
}
private void button1_Click(object sender, System.EventArgs e)
{
Thread td=new Thread(new ThreadStart(TheadS));//创建另一个线程启动Label并无变化。
td.Start();
} private void button2_Click(object sender, System.EventArgs e)
{
TheadS();// 直接启动显示正常。
}
}
}//--------------------Class1---------------------
using System;
using System.Windows.Forms;
namespace WindowsApplication2
{
public class Class1
{
private System.Windows.Forms.Timer timer1=new Timer();
private int i=0;
public Class1()
{
this.timer1.Tick+=new EventHandler(timer1_Tick);
this.timer1.Interval=500; }
public void Start()
{
this.timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
Form1.label_S.Text=(i++).ToString();
}
}
}
// 为清楚起见,省略了一些成员
public void btnAdd_OnClick(object sender, EventArgs e)
{
// 在创建 _txtSum 的线程上执行 UpdateSumDisplay
_txtSum.Invoke(new EventHandler(UpdateSumDisplay));
}
public void UpdateSumDisplay(object sender, EventArgs e)
{
_txtSum.Text = _sum.ToString();
}
具体如下
定义一个委托
delegate void UpdateSumDisplay();
然后修改button1_Click的代码为如下
label1.BeginInvoke(new UpdateSumDisplay(TheadS));
{
System.Threading.Thread tNew = new System.Threading.Thread (new System.Threading.ThreadStart(this.Test));
tNew.Start();
}delegate void SetVisibleDelegate();private void SetVisible()
{
this.button1.Visible = true;
}private void Test()
{
this.Invoke(new SetVisibleDelegate(SetVisible));
}
[DllImport("kernel32.dll")]
public static extern bool Beep(int frequency, int duration);
System.Windows.Forms.Timer
实现在用户定义的时间间隔引发事件的计时器。此计时器最宜用于 Windows 窗体应用程序中,并且必须在窗口中使用。
Form1.Update();界面没更新,更新一下,值就变了。