100分,C#跨线程控件互操作

Taiyangchen 2009-12-09 01:19:41
我在从一个设备获取消息,这个消息的内容有好多种内容,现在我把获取消息专门写在一个类下面假设叫getMsg;我现在需要将获取到的消息分别对应的赋给我的一个Form窗体的label控件上,这个要用到事件委托,多线程知识,我在网上查了查资料,感觉不具有针对性,请大家来帮忙了,给点思路,最好附上代码,这个用到线程知识的,跨线程操作,如何将我的getMsg类里获取到的值赋值给Form界面的控件上呢,谢谢大家!
...全文
304 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
sxfcw 2010-02-26
  • 打赏
  • 举报
回复
学习~~~~~~~~~
healer_kx 2009-12-09
  • 打赏
  • 举报
回复
这个。。。
原理性质的东西,都在我的blog里面,更好的办法,还是用我写的框架吧。
http://www.cnblogs.com/healerkx/category/199400.html
LutzMark 2009-12-09
  • 打赏
  • 举报
回复
如果你不加上Form.CheckForIllegalCrossThreadCalls=false ;
点击第一个按钮。会出现以下异常
线程间操作无效: 从不是创建控件“textBox1”的线程访问它。
LutzMark 2009-12-09
  • 打赏
  • 举报
回复
这个例子把以上3种线程安全与非线程安全的方式全写出来了

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

namespace winform_线程安全与非线程安全
{
public class Form1 : Form
{
// This delegate enables asynchronous calls for setting
// the text property on a TextBox control.
delegate void SetTextCallback(string text);

// This thread is used to demonstrate both thread-safe and
// unsafe ways to call a Windows Forms control.
private Thread demoThread = null;

// This BackgroundWorker is used to demonstrate the
// preferred way of performing asynchronous operations.
private BackgroundWorker backgroundWorker1;

private TextBox textBox1;
private Button setTextUnsafeBtn;
private Button setTextSafeBtn;
private Button setTextBackgroundWorkerBtn;

private System.ComponentModel.IContainer components = null;

public Form1()
{
InitializeComponent();
//Form.CheckForIllegalCrossThreadCalls = false;
}

protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

// This event handler creates a thread that calls a
// Windows Forms control in an unsafe way.
private void setTextUnsafeBtn_Click(
object sender,
EventArgs e)
{
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcUnsafe));

this.demoThread.Start();
}

// This method is executed on the worker thread and makes
// an unsafe call on the TextBox control.
private void ThreadProcUnsafe()
{
this.textBox1.Text = "This text was set unsafely.";
MessageBox.Show(this.textBox1.Text);
}

// This event handler creates a thread that calls a
// Windows Forms control in a thread-safe way.
private void setTextSafeBtn_Click(
object sender,
EventArgs e)
{
this.demoThread =
new Thread(new ThreadStart(this.ThreadProcSafe));

this.demoThread.Start();
}

// This method is executed on the worker thread and makes
// a thread-safe call on the TextBox control.
private void ThreadProcSafe()
{
this.SetText("This text was set safely.");
MessageBox.Show(this.textBox1.Text);
}

// This method demonstrates a pattern for making thread-safe
// calls on a Windows Forms control.
//
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.

private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}

// This event handler starts the form's
// BackgroundWorker by calling RunWorkerAsync.
//
// The Text property of the TextBox control is set
// when the BackgroundWorker raises the RunWorkerCompleted
// event.
private void setTextBackgroundWorkerBtn_Click(
object sender,
EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync();
}

// This event handler sets the Text property of the TextBox
// control. It is called on the thread that created the
// TextBox control, so the call is thread-safe.
//
// BackgroundWorker is the preferred way to perform asynchronous
// operations.

private void backgroundWorker1_RunWorkerCompleted(
object sender,
RunWorkerCompletedEventArgs e)
{
this.textBox1.Text =
"This text was set safely by BackgroundWorker.";
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.setTextUnsafeBtn = new System.Windows.Forms.Button();
this.setTextSafeBtn = new System.Windows.Forms.Button();
this.setTextBackgroundWorkerBtn = new System.Windows.Forms.Button();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(240, 20);
this.textBox1.TabIndex = 0;
//
// setTextUnsafeBtn
//
this.setTextUnsafeBtn.Location = new System.Drawing.Point(15, 55);
this.setTextUnsafeBtn.Name = "setTextUnsafeBtn";
this.setTextUnsafeBtn.TabIndex = 1;
this.setTextUnsafeBtn.Text = "Unsafe Call";
this.setTextUnsafeBtn.Click += new System.EventHandler(this.setTextUnsafeBtn_Click);
//
// setTextSafeBtn
//
this.setTextSafeBtn.Location = new System.Drawing.Point(96, 55);
this.setTextSafeBtn.Name = "setTextSafeBtn";
this.setTextSafeBtn.TabIndex = 2;
this.setTextSafeBtn.Text = "Safe Call";
this.setTextSafeBtn.Click += new System.EventHandler(this.setTextSafeBtn_Click);
//
// setTextBackgroundWorkerBtn
//
this.setTextBackgroundWorkerBtn.Location = new System.Drawing.Point(177, 55);
this.setTextBackgroundWorkerBtn.Name = "setTextBackgroundWorkerBtn";
this.setTextBackgroundWorkerBtn.TabIndex = 3;
this.setTextBackgroundWorkerBtn.Text = "Safe BackgroundWorker Call";
this.setTextBackgroundWorkerBtn.Click += new System.EventHandler(this.setTextBackgroundWorkerBtn_Click);
//
// backgroundWorker1
//
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
//
// Form1
//
this.ClientSize = new System.Drawing.Size(268, 96);
this.Controls.Add(this.setTextBackgroundWorkerBtn);
this.Controls.Add(this.setTextSafeBtn);
this.Controls.Add(this.setTextUnsafeBtn);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion


[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form1());
}

}
}


LutzMark 2009-12-09
  • 打赏
  • 举报
回复
三种方式
1.直接开新Thread,不推荐,这种方式非线程安全,要用必须把 Form.CheckForIllegalCrossThreadCalls=false
2.异步委托,正如龙宜坡所举的例子
3.BackgroundWorker组件,推荐

可以看看微软MSDN的线程安全和非常线程安全那章例子,很全面
龙宜坡 2009-12-09
  • 打赏
  • 举报
回复
LZ单步跟一下会更清楚!
龙宜坡 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 wangkuang5 的回复:]
label1 .BeginInvoke  (new MethodInvoker ( delegate (){ label1.Text = i.ToString ();}));

一句话就可以了 ,i.ToString ()换成你要赋的值

原理你看这里的blog
http://www.cnblogs.com/joechen/archive/2009/04/28/1445425.html
[/Quote]

当然,代码可以写的很简单,但是写的较为规范或高效还是要费一番功夫的。


if (this.richTextBox1.InvokeRequired)//这个判断比较关键。
{//调用方在另外一个线程中
//在拥有此控件的线程上执行委托指向的方法
this.richTextBox1.Invoke(m_handler, txt);//这里其实是在调用方法自身。
}
else
{//调用方在当前线程中
this.richTextBox1.AppendText(txt + "\r\n");//增加文本
this.richTextBox1.ScrollToCaret();//滚动到当前目录
}
wangkuang5 2009-12-09
  • 打赏
  • 举报
回复
label1 .BeginInvoke (new MethodInvoker ( delegate (){ label1.Text = i.ToString ();}));

一句话就可以了 ,i.ToString ()换成你要赋的值

原理你看这里的blog
http://www.cnblogs.com/joechen/archive/2009/04/28/1445425.html
悔说话的哑巴 2009-12-09
  • 打赏
  • 举报
回复
帮顶
龙宜坡 2009-12-09
  • 打赏
  • 举报
回复
其实用BackGroundWorker更好!
woshifou 2009-12-09
  • 打赏
  • 举报
回复
学习。
Taiyangchen 2009-12-09
  • 打赏
  • 举报
回复
我就是在看1楼的
qqiuzaihui 2009-12-09
  • 打赏
  • 举报
回复
UP, 1楼的已经写的很详细了.
龙宜坡 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 deyter 的回复:]
用委托,下面是个简单的例子
C# codeprivatedelegatevoid FlushClient();//代理privatevoid Form1_Load(object sender, EventArgs e)
{
Thread thread=new Thread(CrossThreadFlush);
thread.IsBackground=true;
thread.Start();
}privatevoid CrossThreadFlush()
{while (true)
{
Thread.Sleep(1000);
ThreadFunction();
}
}privatevoid ThreadFunction()
{if (this.textBox1.InvokeRequired)
{
FlushClient fc=new FlushClient(ThreadFunction);this.Invoke(fc);
}else
{this.textBox1.Text= DateTime.Now.ToString();
}
}
[/Quote]

每次都 new 一个,感觉不太好吧!
龙宜坡 2009-12-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ahsun1987 的回复:]
1楼的朋友,能说详细点吗
[/Quote]

这是个简单的示例,代码中的注释部分已经很清楚了,还有哪里不明白或不满足LZ的需求的?

难道非要项目源文件吗?
deyter 2009-12-09
  • 打赏
  • 举报
回复
用委托,下面是个简单的例子

private delegate void FlushClient();//代理
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(CrossThreadFlush);
thread.IsBackground = true;
thread.Start();
}

private void CrossThreadFlush()
{
while (true)
{
Thread.Sleep(1000);
ThreadFunction();
}
}
private void ThreadFunction()
{
if (this.textBox1.InvokeRequired)
{
FlushClient fc = new FlushClient(ThreadFunction);
this.Invoke(fc);
}
else
{
this.textBox1.Text = DateTime.Now.ToString();
}
}


ali4130000 2009-12-09
  • 打赏
  • 举报
回复
WindowsApplication1.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();


写这个试试
Taiyangchen 2009-12-09
  • 打赏
  • 举报
回复
1楼的朋友,能说详细点吗
yuanhuiqiao 2009-12-09
  • 打赏
  • 举报
回复
加载更多回复(3)

111,120

社区成员

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

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

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