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

    Red face Threading in a loop?...possible?

    Basically I want to enumerate a bunch of files in a directory and run a thread on each file only once. So when I use a file I decrease the count by 1, and keep running till 0.

    My understanding of threading is a little weak, but can I not just make an infinte loop until count =0? and when Threadstate=Stopped re-start it? (.net 2.0)
    Code:
                do
                {
                    filename = files[count];
                    if (t1.ThreadState == ThreadState.Stopped)
                    {
                        t1.Start();
                    }
    
                    if (count == 0)
                        writer.Close();
                } while (count != 0);
    (count-- in the thread function)

    Or am I going about this the wrong way...

    Thanks,
    Steve

  2. #2
    Join Date
    Oct 2003
    Posts
    133

    Re: Threading in a loop?...possible?

    Sorry, should prob say why it's not working as shown...

    System.Threading.ThreadStateException was unhandled
    Message="Thread is running or terminated; it cannot restart."

    It enters the IF, then generates this on t1.Start()

  3. #3
    Join Date
    Feb 2006
    Posts
    8

    Re: Threading in a loop?...possible?

    There are few things wrong in your code.
    First, you cannot restart a thread. Once it is terminated, aborted or still running you cannot reuse it.

    Second and more important, I think you misunderstood the concept of threads.

    What your code is doing is running a thread for the first time, the loop continues without waiting for the thread to finish and then trying to restart the same thread while it is probably still running. That's why you get the error.

    You have two options: You can wait for the thread to finish and then continue to loop and then create a new thread to handle the next file; BUT there is no added value using a thread like this. The meaning of threads is that you can handle all the files or some of the files at the same time without waiting for one file to finish.

    You should loop on your file list and on each file create a new thread to handle it. Then, if you want to, you can wait for all threads to finish.

    Your code should look something like:

    for (int i = 0; i < files.Length; i++)
    {
    // get the filename
    string filename = files[i];

    // create the thread. func is the thread function
    Thread t = new Thread(func);

    // start the thread while passing it the filename
    t.Start(filename);
    }

    // here you can wait for all threads to finish (if you need to)
    ...


    Your thread function should look like this:
    public void func(string filename)
    {
    // handle the file
    ...
    }

    I suggest you read more about threads. Reading will make you understand the power of threads and then you would be able to do great work with them.

    Enjoy.

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