I am completely unfamiliar with WPF, I created a simple WPF application that lists the entire drive structure (folder, files) in TreeView, since this process takes some time when I tried to use a thread to run the GetFolderTree () method and do not let the user the interface stops responding, but I have to deal with some problems, I created a class called FolderBrowser, where I have all this assembly code for the cumulative structure, inside this class I create a new instance of TreeViewItem that contains Keturah drive at the end is used as a return value to populate the TreeView, is the code:
using System.IO;
using System.Windows.Controls;
namespace WpfApplication
{
public class FolderBrowser
{
private TreeViewItem folderTree;
private string rootFolder;
public FolderBrowser(string path)
{
rootFolder = path;
folderTree = new TreeViewItem();
}
private void GetFolders(DirectoryInfo di, TreeViewItem tvi)
{
foreach (DirectoryInfo dir in di.GetDirectories())
{
TreeViewItem tviDir = new TreeViewItem() { Header = dir.Name };
try
{
if (dir.GetDirectories().Length > 0)
GetFolders(dir, tviDir);
tvi.Items.Add(tviDir);
GetFiles(dir, tviDir);
}
}
if (rootFolder == di.FullName)
{
folderTree.Header = di.Name;
GetFiles(di, folderTree);
}
}
private void GetFiles(DirectoryInfo di, TreeViewItem tvi)
{
foreach (FileInfo file in di.GetFiles())
{
tvi.Items.Add(file.Name);
}
}
public TreeViewItem GetFolderTree()
{
DirectoryInfo di = new DirectoryInfo(rootFolder);
if (di.Exists)
{
GetFolders(di, folderTree);
}
return folderTree;
}
}
}
How can I create new control instances inside this new thread?