How to use separate thread to iterate through file system and display results in a TreeView?

Hi!

A while ago I wrote a tool to analyze the size of folders on the harddrive using .NET FW 1.1.

The time has come to port it to .Net FW 2.0 and at the same time implement some improvements.

One of the things I would like to do is to let a separate thread iterate throgh the folders of the filesystem so that the user interface does not lock while the iteration, which takes quite some time, is being performed.

For this purpose I am using a BackgroundWorker. This is where I get problems. I am letting the worker build up the folder tree in a TreeView. However, I realized that threads are not allowed to call methods of controls which "belong" to the Mainform, and I am now wondering how I should solve this.

I guess I could let the thread build up a separate tree, and return that enourmous tree after it has completed iterate it, and then copy this tree to the TreeView. This however does not seem like a slim way.

Does anyone have suggestions

Thanks for any tips!


Answer this question

How to use separate thread to iterate through file system and display results in a TreeView?

  • RickSmithers

    Try putting a windows forms timer on your form which periodically (e.g. every second) retrieves the results from the background worker and displays them, then "clears" the results it just processed. The other option is to have the background worker periodically (e.g. every 250 subfolders) send a message to the main thread asking it to process all available results.

  • Worko

    I guess I will need to do some kind of "hack".

    Letting the worker send bathes of processed data sounds like a reasonable hack, thanks for the tip!

    I'm still open for other suggestions though.

  • How to use separate thread to iterate through file system and display results in a TreeView?