2
當樹節點被添加到樹視圖我想要的,用戶界面不鎖(表格可通過拖放移動...)。我使用的是,但它不起作用。請告訴我如何或幫助我解決這個問題。UI已經鎖定時添加樹節點到TreeView控件throught線程
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 partial class Form1 : Form
{
TreeView tree;
TreeNode root;
Button button;
public Form1()
{
this.Name = "Form1";
this.Text = "Form1";
root = new TreeNode("Hello");
tree = new TreeView();
tree.Location = new Point(0, 0);
tree.Size = new Size(this.Width, this.Height - 70);
tree.Anchor = AnchorStyles.Top | AnchorStyles.Bottom;
this.Controls.Add(tree);
tree.Nodes.Add(root);
button = new Button();
button.Text = "Add nodes";
button.Location = new Point(0, this.Height - 70);
button.Size = new Size(this.Width, 30);
button.Anchor = AnchorStyles.Bottom;
this.Controls.Add(button);
button.Click += new EventHandler(button_Click);
}
void button_Click(object sender, EventArgs e)
{
Thread t = new Thread(new ThreadStart(this.AddNode));
t.Start();
}
void AddNode()
{
if (this.tree.InvokeRequired)
{
this.tree.BeginInvoke(new MethodInvoker(this.AddNodeInternal));
}
else
{
this.AddNodeInternal();
}
}
void AddNodeInternal()
{
root.Collapse();
root.Nodes.Clear();
TreeNode[] nodesToAdd = new TreeNode[20000];
for (int i = 0; i < 20000; i++)
{
System.Threading.Thread.Sleep(1);
TreeNode node = new TreeNode("Node " + i.ToString());
nodesToAdd[i] = node;
}
root.Nodes.AddRange(nodesToAdd);
root.Expand();
}
delegate void AddNodeDelegate();
}
}
但是,這個問題不解決! – dungnn07 2011-04-22 06:24:42