M
mycwcgr_new
Unregistered / Unconfirmed
GUEST, unregistred user!
C#中,在多线程中,为什么子线程操作主界面的某些控件正常,而操作某些控件产生错误?
在子线程的private void FillListView()操作中,如果执行listBox1.Items.Add(i);则正常,而执行treeView1.Nodes.Add(new TreeNode(i.ToString()));时产生下面的错误,为什么?
“在该控件上执行的操作正被错误的线程调用。必须使用 Control.Invoke 或 Control.begin
Invoke 封送到正确的线程才能执行此操作。”
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace WindowsApplication6
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TreeView treeView1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(104, 192);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(16, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(112, 112);
this.listBox1.TabIndex = 1;
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(144, 24);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(120, 112);
this.treeView1.TabIndex = 2;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
ThreadStart worker=new ThreadStart(FillListView);
Thread workThread=new Thread(worker);
workThread.Start();
}
private void FillListView()
{
for(int i=0;i<=50;i++)
{
listBox1.Items.Add(i);
//treeView1.Nodes.Add(new TreeNode(i.ToString()));
}
}
}
}
在子线程的private void FillListView()操作中,如果执行listBox1.Items.Add(i);则正常,而执行treeView1.Nodes.Add(new TreeNode(i.ToString()));时产生下面的错误,为什么?
“在该控件上执行的操作正被错误的线程调用。必须使用 Control.Invoke 或 Control.begin
Invoke 封送到正确的线程才能执行此操作。”
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
namespace WindowsApplication6
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TreeView treeView1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
this.button1.Location = new System.Drawing.Point(104, 192);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(16, 24);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(112, 112);
this.listBox1.TabIndex = 1;
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(144, 24);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(120, 112);
this.treeView1.TabIndex = 2;
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.treeView1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
ThreadStart worker=new ThreadStart(FillListView);
Thread workThread=new Thread(worker);
workThread.Start();
}
private void FillListView()
{
for(int i=0;i<=50;i++)
{
listBox1.Items.Add(i);
//treeView1.Nodes.Add(new TreeNode(i.ToString()));
}
}
}
}