首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

线程访问winform界面控件的事例

2012-11-19 
线程访问winform界面控件的例子本例子主要是针对解决异常: 线程间操作无效: 从不是创建控件“richTextBox1”

线程访问winform界面控件的例子

本例子主要是针对解决异常:

线程间操作无效: 从不是创建控件“richTextBox1”的线程访问它


关键看红色的代码,详细的查MSDN.


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 WindowsFormsApplication1
{
??? public delegate void MyInitDelegate(RichTextBox myRtb, string strTemp);

??? public partial class Form1 : Form
??? {

??????? public Form1()
??????? {
??????????? InitializeComponent();

??????? }

??????? private void Form1_Load(object sender, EventArgs e)
??????? {
??????????? Thread t = new Thread(new ThreadStart(this.output));
??????????? t.Start();
??????? }

??????? private void output()
??????? {
??????????? string message = "pppp";
??????????? this.richTextBox1.BeginInvoke(new MyInitDelegate(DelegateInitMethod), new object[] { this.richTextBox1, message });
??????? }

??????? public void DelegateInitMethod(RichTextBox myRtb, string strTemp)
??????? {
??????????? myRtb.AppendText(System.Environment.NewLine + strTemp);
??????? }

??? }
}

?

热点排行