Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1095365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:06:45+00:00 2026-05-17T00:06:45+00:00

Okay, I have a TreeView that serves as a directory tree for Windows. I

  • 0

Okay, I have a TreeView that serves as a directory tree for Windows. I have it loading all the directories and functioning correctly, but it is pausing my GUI while it loads directories with many children. I’m trying to implement multithreading, but am new to it and am having no luck.

This is what I have for my TreeView:

private readonly object _dummyNode = null;

public MainWindow()
{
    InitializeComponent();

    foreach (string drive in Directory.GetLogicalDrives())
    {
        DriveInfo Drive_Info = new DriveInfo(drive);                

        if (Drive_Info.IsReady == true)
        {
            TreeViewItem item = new TreeViewItem();
            item.Header = drive;
            item.Tag = drive;
            item.Items.Add(_dummyNode);
            item.Expanded += folder_Expanded;

            TreeViewItemProps.SetIsRootLevel(item, true);

            Dir_Tree.Items.Add(item);
        }
    }
}

private void folder_Expanded(object sender, RoutedEventArgs e)
{
    TreeViewItem item = (TreeViewItem)sender;

    if (item.Items.Count == 1 && item.Items[0] == _dummyNode)
    {
        item.Items.Clear();

        try
        {
            foreach (string dir in Directory.GetDirectories(item.Tag as string))
            {
                DirectoryInfo tempDirInfo = new DirectoryInfo(dir);

                bool isSystem = ((tempDirInfo.Attributes & FileAttributes.System) == FileAttributes.System);

                if (!isSystem)
                {
                    TreeViewItem subitem = new TreeViewItem();
                    subitem.Header = tempDirInfo.Name;
                    subitem.Tag = dir;
                    subitem.Items.Add(_dummyNode);
                    subitem.Expanded += folder_Expanded;
                    subitem.ToolTip = dir;
                    item.Items.Add(subitem);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }
}

Whenever I expand a directory that has a large number of subdirectories, the program appears to be frozen for a few seconds. I would like to display a loading message or animation while it’s processing, but I’m not sure how to begin with multithreading. I know I have to use the TreeView’s Dispatcher.BeginInvoke method, but other than that I’m kinda lost.

Any help would be greatly appreciated!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:06:46+00:00Added an answer on May 17, 2026 at 12:06 am

    One of the easiest ways to start a async process is to use an anonymous delegate with BeginInvoke. As an example you could move your code in the constructor to a separate method (say RenderTreeView) and then call it asynchronously to begin a new thread as follows:

    Action action = RenderTreeView;
    action.BeginInvoke(null, null);
    

    The trick to this is that any time you interact with any UI elements from the async process you need to rejoin the main UI thread, otherwise you will get an exception about cross thread access. This is relatively straight forward as well.

    In Windows Forms it’s:

    if (InvokeRequired)
        Invoke(new MethodInvoker({item.Items.Add(subitem)}));
    else
        item.Items.Add(subitem);
    

    In WPF it’s:

    if (!Dispatcher.CheckAccess())
        Dispatcher.Invoke(new Action(() => item.Items.Add(subitem)));
    else
        item.Items.Add(subitem);
    

    You really need to break up the code to make it more flexible in terms of methods. At the moment everything is bundled in one method which makes it hard to work with and re-factor for async processes.

    Update Here you go 🙂

    public partial class MainWindow : Window
    {
        private readonly object dummyNode = null;
    
        public MainWindow()
        {
            InitializeComponent();
    
            Action<ItemCollection> action = RenderTreeView;
            action.BeginInvoke(treeView1.Items, null, null);
        }
    
        private void RenderTreeView(ItemCollection root)
        {
            foreach (string drive in Directory.GetLogicalDrives())
            {
                var driveInfo = new DriveInfo(drive);
    
                if (driveInfo.IsReady)
                {
                    CreateAndAppendTreeViewItem(root, drive, drive, drive);
                }
            }
        }
    
        private void FolderExpanded(object sender, RoutedEventArgs e)
        {
            var item = (TreeViewItem) sender;
    
            if (item.Items.Count == 1 && item.Items[0] == dummyNode)
            {
                item.Items.Clear();
                var directory = item.Tag as string;
                if (string.IsNullOrEmpty(directory))
                {
                    return;
                }
                Action<TreeViewItem, string> action = ExpandTreeViewNode;
                action.BeginInvoke(item, directory, null, null);
            }
        }
    
        private void ExpandTreeViewNode(TreeViewItem item, string directory)
        {
            foreach (string dir in Directory.GetDirectories(directory))
            {
                var tempDirInfo = new DirectoryInfo(dir);
    
                bool isSystem = ((tempDirInfo.Attributes & FileAttributes.System) == FileAttributes.System);
    
                if (!isSystem)
                {
                    CreateAndAppendTreeViewItem(item.Items, tempDirInfo.Name, dir, dir);
                }
            }
        }
    
        private void AddChildNodeItem(ItemCollection collection, TreeViewItem subItem)
        {
            if (Dispatcher.CheckAccess())
            {
                collection.Add(subItem);
            }
            else
            {
                Dispatcher.Invoke(new Action(() => AddChildNodeItem(collection, subItem)));
            }
        }
    
        private void CreateAndAppendTreeViewItem(ItemCollection items, string header, string tag, string toolTip)
        {
            if (Dispatcher.CheckAccess())
            {
                var subitem = CreateTreeViewItem(header, tag, toolTip);
                AddChildNodeItem(items, subitem);
            }
            else
            {
                Dispatcher.Invoke(new Action(() => CreateAndAppendTreeViewItem(items, header, tag, toolTip)));
            }
        }
    
        private TreeViewItem CreateTreeViewItem(string header, string tag, string toolTip)
        {
            var treeViewItem = new TreeViewItem {Header = header, Tag = tag, ToolTip = toolTip};
    
            treeViewItem.Items.Add(dummyNode);
            treeViewItem.Expanded += FolderExpanded;
    
            return treeViewItem;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.