CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2005
    Posts
    3

    Question 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.

  2. #2
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    1,080

    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.

  3. #3
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    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

  4. #4
    Join Date
    Sep 2005
    Posts
    3

    Question 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?

  5. #5
    Join Date
    Jun 2004
    Location
    New Jersey, USA
    Posts
    341

    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
  •  





Click Here to Expand Forum to Full Width

Featured