Click to See Complete Forum and Search --> : Multithreading: Creating Threads from Within a Thread


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.

jmcilhinney
September 28th, 2005, 10:06 PM
You could place all your newly created threads in an array or collection when they are created. You could then Join your UI thread to the first thread. When that thread terminates you would look through the array or collection for the next thread that is still executing and Join to that. You would keep doing this until there were no more threads that were still executing. Make sure you synchronise access to the array or collection though.

stardv
September 29th, 2005, 08:55 AM
Or you can use event signaling between thread using ManualResetEvent and AutoResetEvent to communicate betwwen events and signal to the UI event when all of them are completed

tkrampe
September 29th, 2005, 11:47 AM
Thanks for the replies. Do know of any tutorials or examples that cover either of these methods?

stardv
September 29th, 2005, 12:41 PM
Take a look here:

1. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingmanualreseteventclasstopic.asp

2.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingautoreseteventclasstopic.asp