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 997769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:05:15+00:00 2026-05-16T07:05:15+00:00

I am totally new to WPF, I have created a simple WPF app that

  • 0

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

  • 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-16T07:05:16+00:00Added an answer on May 16, 2026 at 7:05 am

    You can’t interact with the UI in any thread but the UI thread, but you can use the UI Dispatcher object to execute a callback inside the UI thread:

    System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() => { /* your UI code here */ }));
    

    A more “clean” way of obtaining the dispatcher is to pass it from the UI object to the thread/class that spawns the thread, when you are creating it.

    Edit:

    I recommend HCL’s solution over mine. However, you asked in comments how to get this to work without duplicating this big nasty block of code everywhere:

    In your constructor, take a reference to a Dispatcher object, and store it within your class.

    Then make a method like this:

    private void RunOnUIThread(Action action)
    {
        this.dispatcher.Invoke(action);
    }
    

    And call it like this:

    RunOnUIThread(() => { /* UI code */ });
    

    You can wrap large blocks of code this way:

    RunOnUIThread(() =>
    {
      Console.WriteLine("One statement");
      Console.WriteLine("Another statement");
    });
    

    If you try to push too much of this code back into the UI, though, it will be no different than if you executed all the code within the UI thread, and will still hang the UI.

    However, HCL’s suggestion of populating a custom tree structure, instead of having that code know anything about UI controls, is much better 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am totally new to SQL . I have a simple select query similar
Okay, I'm totally new to Python, so I decided to make a simple app.
My question is straight forward. I'm totally new to the Microsoft stack, I have
Totally new to Cake. I have this table named Content and another one named
im totally new to rails. here my question: i made an app with articles
I'm totally new to ajax, but pretty good with PHP. I have a PHP
I am totally new to programming and I have chosen Delphi as the programming
I am totally new to XSLT and can't work out where I am going
Are there any good books for a relatively new but not totally new *nix
I've heard that unit testing is totally awesome, really cool and all manner of

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.