C# 子线程怎么操作主线程的组件??

stm32w 2010-10-31 02:25:40
C# 子线程怎么操作主线程的组件??

比如说主线程有个 textBox,

子线程要往textBox里面追加一个字符串,怎么实现?
...全文
345 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
grzx2210 2010-11-01
  • 打赏
  • 举报
回复
winform就需要 一部调用 代理。 Wpf 这有this.Dispatcher.Invoke
scholar_fly 2010-11-01
  • 打赏
  • 举报
回复
使用委托。委托可以传递class的指针
  • 打赏
  • 举报
回复
归归,顶!
  • 打赏
  • 举报
回复

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());
}
}
}
S_elva 2010-10-31
  • 打赏
  • 举报
回复
5L 正解。
让爱延续 2010-10-31
  • 打赏
  • 举报
回复

Control.Invoke或Control.BeginInvoke方法
wuyq11 2010-10-31
  • 打赏
  • 举报
回复
需要通过 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");
}
}
MOTA 2010-10-31
  • 打赏
  • 举报
回复
委托.
nfdz2008 2010-10-31
  • 打赏
  • 举报
回复
呵呵 还是不懂
边城的刀声 2010-10-31
  • 打赏
  • 举报
回复

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";
}
边城的刀声 2010-10-31
  • 打赏
  • 举报
回复
这和线程不线程没有关系吧。
如果线程启用的方法,不在当前页面,你可以把参数传递进去.

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧