C # stream starts from debugger but does not start autonomously
I have a small application that looks up and stores the names of a large number of files on startup. I split this search into multiple Thread objects, each looking for a single directory and returning the results to the main thread.
When the app loads, I go through each thread and load it:
foreach(Thread t in m_threads)
{
t.Start();
while(!t.IsAlive){}
}
When I start this application in the debugger, it loads and the threads find all the files quickly. However, if I start outside of the debugger, it freezes up. Attaching a debugger to a process I see that IsAlive never returns true.
I'm new to C # streaming, so does anyone know what's going wrong, or how can I more easily debug what's going on?
source to share
It can't be exactly related to why things are freezing, but your implementation is highly questionable. Are you enumerating a collection of threads, starting each one, but then blocking until the thread is finished? I think what you might want to do is start all threads and then block until all threads are finished. (Note: This assumes you meant "while (t.IsAlive) {}", since waiting for the stream to start makes even less sense.)
As far as freezing is concerned, we may need to see the rest of the code. Since you said that you are new to C # threading, and looking at what you were doing above, I am assuming that you are also new to threading, which means there could be many places that could arise.
source to share
The thread can complete processing before you check with IsAlive.
Why are you waiting while checking the IsAlive flag?
Why not start all threads and then call Join () in another loop to wait for the threads to end? You don't need to check "IsAlive".
Better yet, why not use a ThreadPool instead of starting the thread stack yourself?
source to share
It looks like I misunderstood the code I cut-and-paste to create my stream code. I assumed that the while (! T.isAlive) {} line was blocking until the thread could start normally on its own, and I thought it was necessary for administration.
I removed this line and the application runs fine from the debugger. I am joining threads later in the code, so this should be it.
Is there a "NEWB!" tag? :)
source to share
With the code you provided above, it seems to me that you want to run threads sequentially. If so, why do you need a lot of threads? 1 can do the job.
EDIT: (after your answer)
Ok. I see that you removed the questionable line.
However, in your case, I would like to use only one stream to load these filenames, because you are only accessing one I / O resource (disk). Each thread competes for this resource. Have you tried to move 1 large file to windows and then move another large file while the first one is still moving? Your disk performance decreases. Because of this, I would only create one thread for every other I / O resource.
source to share
I would recommend not storing the Threads collection, but instead using Threadstarts. If you join them later, you don't need to wait for the thread to run, so you need a while loop. Make sure your threads are marked as Background-Threads so they are automatically cleaned up after execution.
I would do it like this: List threadStarters = new List ();
foreach (ThreadStart ts in threadStarters)
{
Thread t = new Thread(ts);
t.IsBackground = true;
t.Start();
}
source to share