C# 子线程怎么操作主线程的组件?? C# 子线程怎么操作主线程的组件??比如说主线程有个 textBox,子线程要往textBox里面追加一个字符串,怎么实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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"; } 需要通过 Control.Invoke或Control.BeginInvoke方法来进行处理Control.CheckForIllegalCrossThreadCalls=false;delegate void AddDelegate(); event AddDelegate addEvent; private void Form1_Load(object sender, EventArgs e) { addEvent+= new AddDelegate(TEST); Thread myThread = new Thread(new ThreadStart(this.Add)); myThread.Start(); } private void TEST() { listBox1.Items.Add("a"); } private void Add() { if (this.InvokeRequired) { this.Invoke(addEvent); } else { listBox1.Items.Add("ip"); } } 顶 Control.Invoke或Control.BeginInvoke方法 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()); } }} 使用委托。委托可以传递class的指针 winform就需要 一部调用 代理。 Wpf 这有this.Dispatcher.Invoke 莫名其妙偶尔会很慢 求中文翻译,在线等.... 大家用什么版本控制工具?我一直用的是TortoiseSVN,但有疑问 int a = 1和 int b= new int ()有什区别 C# AJAX验证错误? 100分求视频播放器源码(仿暴风影音) ===使用ASP.NET连接MYSQL4.1出现乱码=== 100分求端口映射问题~~ Help Help datagird与序号???? c#实现导出Excel的下拉菜单 【WPF】 RichTextBox和ListBox几个问题。
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";
}
Control.CheckForIllegalCrossThreadCalls=false;delegate void AddDelegate();
event AddDelegate addEvent; private void Form1_Load(object sender, EventArgs e)
{
addEvent+= new AddDelegate(TEST);
Thread myThread = new Thread(new ThreadStart(this.Add));
myThread.Start();
}
private void TEST()
{
listBox1.Items.Add("a");
}
private void Add()
{
if (this.InvokeRequired)
{
this.Invoke(addEvent);
}
else
{
listBox1.Items.Add("ip");
}
}
Control.Invoke或Control.BeginInvoke方法
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());
}
}
}