I'm creating a windows explorer-like application using a treeview control. When a user double clicks a treeview item, it reloads the treeview with the new directory and its subfolders and files. Works fine.
I tried to add a progress bar to it and have it update using the backgroundworker object, like this msdn example:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
using this code for the mouse double click event of the treeview items:
and this for the background worker eventhandlers:
I get an error stating "The calling thread must be STA, because many UI components require this." when I run it on the line that instantiates the first treeview item in my do work event handler
"RadTreeViewItem tvDirectoryItem = new RadTreeViewItem() { Header = dInfo.Name, DefaultImageSrc = "folder_icon.gif", ItemsOptionListType = OptionListType.OptionList, OptionType = OptionListType.None };"
Help please, thanks.
I tried to add a progress bar to it and have it update using the backgroundworker object, like this msdn example:
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
using this code for the mouse double click event of the treeview items:
private void tvDirectory_MouseDoubleClick(object sender, MouseButtonEventArgs e){ RadTreeViewItem item = (RadTreeViewItem)((RadTreeView)sender).SelectedContainer; string arg = item.Header.ToString(); wrkDeploy.RunWorkerAsync(arg);}and this for the background worker eventhandlers:
BackgroundWorker wrkDeploy; public MainWindow() { InitializeComponent(); this.wrkDeploy = new BackgroundWorker(); wrkDeploy.RunWorkerCompleted += new RunWorkerCompletedEventHandler(wrkDeploy_RunWorkerCompleted); wrkDeploy.DoWork += new DoWorkEventHandler(wrkDeploy_DoWork); wrkDeploy.ProgressChanged += new ProgressChangedEventHandler(wrkDeploy_ProgressChanged); } private void wrkDeploy_DoWork(object sender, DoWorkEventArgs e) { string item = e.Argument.ToString(); if (isDirectory(_currentPath + item.ToString())) { _currentPath += @"\"; DirectoryInfo di = new DirectoryInfo(_currentPath); DirectoryInfo[] directories = null; int cnt = 0; int maxCnt = 0; try { directories = di.GetDirectories(); FileInfo[] files = di.GetFiles(); maxCnt = directories.Length + files.Length; while (cnt < maxCnt) { foreach (DirectoryInfo dInfo in directories) { RadTreeViewItem tvDirectoryItem = new RadTreeViewItem() { Header = dInfo.Name, DefaultImageSrc = "folder_icon.gif", ItemsOptionListType = OptionListType.OptionList, OptionType = OptionListType.None }; tvDirectory.Items.Add(tvDirectoryItem); cnt += 1; int percentComplete = (cnt / maxCnt) * 100; wrkDeploy.ReportProgress(percentComplete); } if (files.Length > 0 && cnt == 0) { RadTreeViewItem root = new RadTreeViewItem() { Header = "Select/Deselect All Files in Folder" }; root.Checked += new System.EventHandler<Telerik.Windows.RadRoutedEventArgs>(tvDirectory_itemChecked); root.Unchecked += new System.EventHandler<Telerik.Windows.RadRoutedEventArgs>(tvDirectory_itemUnChecked); tvDirectory.Items.Add(root); } foreach (FileInfo fInfo in files) { RadTreeViewItem tvFileItem = new RadTreeViewItem() { Header = fInfo.Name, ItemsOptionListType = OptionListType.CheckList, OptionType = OptionListType.CheckList }; tvFileItem.Checked += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(tvDirectory_fileChecked); tvFileItem.Unchecked += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(tvDirectory_fileUnChecked); tvDirectory.Items.Add(tvFileItem); cnt += 1; int percentComplete = (cnt / maxCnt) * 100; wrkDeploy.ReportProgress(percentComplete); } } } catch (UnauthorizedAccessException uae) { } } } private void wrkDeploy_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } private void wrkDeploy_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.ProgressBar1.Value = (double)e.ProgressPercentage; }I get an error stating "The calling thread must be STA, because many UI components require this." when I run it on the line that instantiates the first treeview item in my do work event handler
"RadTreeViewItem tvDirectoryItem = new RadTreeViewItem() { Header = dInfo.Name, DefaultImageSrc = "folder_icon.gif", ItemsOptionListType = OptionListType.OptionList, OptionType = OptionListType.None };"
Help please, thanks.