111,129
社区成员
发帖
与我相关
我的任务
分享
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
{
#region 界面元素
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;
#endregion
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());
}
}
}
public void StartWithArg()
{
structB stNum = new structB();
stNum.structNum = new structA[100];
Thread thread1 = new Thread(delegate() { Run(txtName); });
thread1.Start();
}
public void Run(TextBox txt)
{
txt.Text += "run";
}