使用委托操作相关控件 Invoke操作 private void Form1_Load(System.Object sender, System.EventArgs e) { System.Threading.Thread td = new System.Threading.Thread(TEST); td.Start(); } public delegate void move(); public void TEST() { if (Label.InvokeRequired == false) { } else {} }
private delegate void Idelegate(Button a,Button b); private void move(Button a, Button b) {
int i = a.Location.X; int n = b.Location.X; int count = 50; while (count>0) { Thread.Sleep(1); a.Location = new Point(i, a.Location.Y); i += 2; b.Location = new Point(n, b.Location.Y); n -= 2; count--; } } private void demo() { if (thread.IsBackground) { move(button1, button2); } else { Idelegate dg = move; //dg.Invoke(); //dg(button1, button2);
Invoke操作
private void Form1_Load(System.Object sender, System.EventArgs e)
{
System.Threading.Thread td = new System.Threading.Thread(TEST);
td.Start();
}
public delegate void move();
public void TEST()
{
if (Label.InvokeRequired == false) {
}
else {}
}
private void move(Button a, Button b)
{
int i = a.Location.X;
int n = b.Location.X;
int count = 50;
while (count>0)
{
Thread.Sleep(1);
a.Location = new Point(i, a.Location.Y);
i += 2;
b.Location = new Point(n, b.Location.Y);
n -= 2;
count--;
}
}
private void demo()
{
if (thread.IsBackground)
{
move(button1, button2);
}
else
{
Idelegate dg = move;
//dg.Invoke();
//dg(button1, button2);
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
thread = new Thread(demo);
thread.Start(); }求帮改下
2.老问题。看博客
http://blog.csdn.net/wuyazhe/archive/2010/10/03/5920107.aspx
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace ThreadLableTest
{
public class Form1 : Form
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.btnStart = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.btnStop = new System.Windows.Forms.Button();
this.SuspendLayout();
this.btnStart.Location = new System.Drawing.Point(176, 107);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 23);
this.btnStart.TabIndex = 0;
this.btnStart.Text = "开始移动";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 39);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(59, 12);
this.label1.TabIndex = 1;
this.label1.Text = "移动Label";
this.btnStop.Location = new System.Drawing.Point(257, 107);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(75, 23);
this.btnStop.TabIndex = 2;
this.btnStop.Text = "停止移动";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(345, 208);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.label1);
this.Controls.Add(this.btnStart);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "线程测试";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.ResumeLayout(false);
this.PerformLayout(); }
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnStop;
private volatile bool _shouldStop;
public Form1()
{
InitializeComponent();
_shouldStop = false; }
private void btnStart_Click(object sender, EventArgs e)
{
_shouldStop = false;
Thread move = new Thread(new ThreadStart(MoveLabel));
move.Start();
}
private delegate void MoveHandler(Point point);
private void DoMove(Point point)
{
this.label1.Location = point;
}
private void MoveLabel()
{
while (!_shouldStop)
{
Point nowPoint = this.label1.Location;
Point newPoint = new Point(nowPoint.X + 1, nowPoint.Y);
this.label1.Invoke(new MoveHandler(DoMove), new object[] { newPoint });
Thread.Sleep(100);
}
}
private void btnStop_Click(object sender, EventArgs e)
{
_shouldStop = true;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_shouldStop = true;
Environment.Exit(0);
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
{
code;
}