|
-
September 28th, 2005, 02:56 PM
#1
Multithreading: Creating Threads from Within a Thread
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.
-
September 28th, 2005, 10:06 PM
#2
Re: Multithreading: Creating Threads from Within a Thread
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.
-
September 29th, 2005, 08:55 AM
#3
Re: Multithreading: Creating Threads from Within a Thread
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
"We act as though comfort and luxury were the chief requirements of
life, when all that we need to make us happy is something to be
enthusiastic about."
- Einstein
-
September 29th, 2005, 11:47 AM
#4
Re: Multithreading: Creating Threads from Within a Thread
Thanks for the replies. Do know of any tutorials or examples that cover either of these methods?
-
September 29th, 2005, 12:41 PM
#5
Re: Multithreading: Creating Threads from Within a Thread
"We act as though comfort and luxury were the chief requirements of
life, when all that we need to make us happy is something to be
enthusiastic about."
- Einstein
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|