tkrampe
September 28th, 2005, 02:56 PM
Hi, I'm a bit new to C# and I had a question about something that I can't figure out and can't seem to find any documentation on. In a program that I am writing I have a bit of code that contacts a server and builds up the folder structure of a given directory(i.e. sub directories, files, etc) and then returns it to the UI. I've designed it to be recursive so basically I have a method that is called from the UI and takes a folder path. It then creates an array of the items within the directory, and then goes through each item. If an item is found to be a directory it then calls the same method again passing it the new folder path so that its subitems can be sorted through. My code works fine, but it is pretty slow especially when you are dealing with a directory with many subdirectories, becuase it has to keep going to the server for information. So I decided to try a multithreaded approach. I want it so that every time this method is called a new thread is created. That way when a sub directory is found a new thread is created and the current thread just continues processing the folder it was currently working on. I was able to implement this and it too works, but I can't seem to figure out how to get my UI to pause till all these threads are done. I can get the UI to wait for the thread that it created to return, but since that thread also created its own worker threads the UI knows nothing about those. I've found many articles that use the thread pool to run several threads and then wait for them to finish, however I can't seem to figure out how to implement this since I'm creating new threads from within the thread that was originally called from the UI. Any help would be appreciated. Thanks.