【发布时间】:2013-09-11 19:14:28
【问题描述】:
这里是完整的源代码:http://pastebin.com/mLaGwwi0 您会注意到,事情是扫描目录和文件并将它们填充到树视图中。我正在使用后台工作者(Threads 类也是如此)在后台执行此操作,同时使用节点一一更新树视图。 问题是它不在后台工作,也没有更新树视图。这是悬挂表单的代码部分:
public void ListDirectory(DirectoryInfo path)
{
treeView1.Nodes.Add(CreateDirectoryNode(path));
}
public void Addnode(DirectoryInfo dirinfo)
{
Invoke(new AddCDAnode(ListDirectory), new object[] { dirinfo });
}
private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
{
var directoryNode = new TreeNode(directoryInfo.Name);
foreach (var directory in directoryInfo.GetDirectories())
{
//Statustext = directory.FullName;
directoryNode.Nodes.Add(CreateDirectoryNode(directory));
}
foreach (var file in directoryInfo.GetFiles())
directoryNode.Nodes.Add(new TreeNode(file.Name));
return directoryNode;
}
public delegate void AddCDAnode(DirectoryInfo dirinfo);
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(@"\\server\trabajos 2013\");
Addnode(dir);
}
private void Form1_Load(object sender, EventArgs e)
{
//Start filling the TreeView on a separate thread
backgroundWorker1.RunWorkerAsync();
}
在完整的源代码中,您将看到带注释的代码 - 来自示例并在后台运行,没有任何麻烦。所以我认为问题在于我的扫描目录代码。
解决方案是also available here,如果你想下载它。
【问题讨论】:
-
那看起来不像源代码...
-
很长,所以我把它贴到 Paste Bin ,你可以在我的消息末尾看到链接。请从问题中删除“减号”并阅读整篇文章。谢谢。
-
wetransfer.com/downloads/… - 这是在 goo.gl 中编码的链接 :)
-
@Oleg.Budeanu 您不应该只是提供大量代码转储并说“修复它”。如果您的代码太多以至于无法将其放入问题中,那么这表明您的代码太多了;您应该努力确定什么是相关的和不相关的,以便您可以包含足够的代码来演示问题,但不包括与问题无关的代码。做一些测试,看看哪些工作正常,哪些工作不能正常工作,以缩小可能不能工作的范围。
-
@Oleg.Budeanu 这就是我帮助你完成它的原因。您刚刚获得了很好的学习体验,可以在以后提出问题时使用它来帮助您,此外还可以将此问题改进到可以回答的程度。
标签: c# treeview backgroundworker directoryinfo