I’ve got a small app that searches for and stores the names of a large number of files at start-up. I’m splitting this search into several Thread objects that each search one directory, and push results back 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 begin this app in the debugger, it loads and the threads quickly find all the files. However if I start outside the debugger it freezes. Attaching the debugger to the process I can see that IsAlive never returns true.
I’m new to C# threading so does anyone have any idea what’s going wrong, or how I can more easily debug what’s happening?
It may not be exactly related to why things are freezing, but your implementation is quite questionable. You enumerate over a collection of threads, start each one, but then block until the thread is finished? I think what you may have meant to do is start all threads, then block until all threads have finished. (Note: This is assuming you meant to write: ‘while(t.IsAlive) {}‘, since waiting for the thread to start makes even less sense.)
As for the freezing, we may need to see the rest of the code. Since you said you are new to C# threading, and looking at what you did above, I am assuming you are also new to threading, which means there could be many places that a problem can rise.