I am totally new to WPF, I have created a simple WPF app that lists whole drive structure (folder, files) to a TreeView, since this process takes a while I tried to use a thread to run the GetFolderTree() method and prevent the UI from becoming unresponsive, however I am facing some problems, I have created a Class named FolderBrowser where I have all that drive structure gathering code, inside that class I create a new instance of TreeViewItem which holds drive structure at the end it is used as return value to populate the TreeView, This 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);
}
//catch code here
}
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 could I create new control instances inside this new thread?
Thanks in advance