在treenode.Nodes.Add(tn1)这一句上为什么抛出错误说:在该控件上执行的操作正从错误的线程调用。使用 Control.Invoke 或 Control.BeginInvoke 封送到正确的线程才能执行此操作
public partial class Form1 : Form
{
public delegate void MyDelegate(DirectoryInfo info, TreeNode treenode);
public MyDelegate MD;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\program files");
TreeNode tn = new TreeNode(); MD = new MyDelegate(PrintRecursive);
MD.BeginInvoke(dinfo, tn, null, null);
treeView1.Nodes.Add(tn);
} protected void PrintRecursive(DirectoryInfo info,TreeNode treenode)
{
TreeNode tn1 = new TreeNode(info.Name);
treenode.Nodes.Add(tn1);
DirectoryInfo[] dinfo2 = info.GetDirectories();
foreach (DirectoryInfo tn in dinfo2)
{
PrintRecursive(tn,tn1);
}
}
}
public partial class Form1 : Form
{
public delegate void MyDelegate(DirectoryInfo info, TreeNode treenode);
public MyDelegate MD;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"C:\program files");
TreeNode tn = new TreeNode(); MD = new MyDelegate(PrintRecursive);
MD.BeginInvoke(dinfo, tn, null, null);
treeView1.Nodes.Add(tn);
} protected void PrintRecursive(DirectoryInfo info,TreeNode treenode)
{
TreeNode tn1 = new TreeNode(info.Name);
treenode.Nodes.Add(tn1);
DirectoryInfo[] dinfo2 = info.GetDirectories();
foreach (DirectoryInfo tn in dinfo2)
{
PrintRecursive(tn,tn1);
}
}
}
解决方案 »
- 很猛 折磨了我一夜
- 怎样从一个字符串中截取文件名
- 怎么设置HttpWebRequset的某个响应状态关闭
- 如何将捕获到的摄像头视频传输给局域网的其它机子,急在线等!!!!
- 特急 !!! word导入文件到richtextbox
- 菜鸟求一算法,关于棋牌游戏的
- vs2008 C#连接 excel 求助
- 小弟没分了,希望各位大哥大嫂告诉我写条SQL语句。谢谢
- .net 混淆器Dotfuscator Community Edition 在ClickOnce方法部署时如何应用?
- 'System.Reflection.TargetInvocationException' 的问题
- TrueType字体的相关知识 怎么编程方式制作自己的字体
- C# Winfrom多窗口播放图片
{
this.lblInfo.Invoke(new SetFileInfomationHande(SetFileInfomation), new object[] { message });
}
{
this.lblInfo.Text = message;
}
delegate void FormShow();
FormShow _FormShow;
并事例化
_FormShow=new FormShow(ThisShow);
private void ThisShow()
{
this.Show();
if (_Dat.Save)
{
SaveDat(Application.StartupPath + "\\MyDat.sd");
}
f2.Close();
t.Abort();
}
线程中操作控件很麻烦必须用Invoke方法传递委托
this.Invoke(_FormShow);