[.net] How to use threads to iterate through file system?

This topic is 4463 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

// This method is constantly run by a thread.
public void RegularlyCheckFolderQueue()
{
while (applicationIsRunning) {
// Check for folders to analyze 10 times each second.
if (AnalysisEngine.ListOfFolderToBuildTreeFrom.Count > 0) {
AnalysisEngine.ListOfFolderToBuildTreeFrom.RemoveFirst();
}
}
}


Am I looking at the problem from the wrong viewpoint? Can it be solved more elegantly (like without static fields)? Any tips appreciated! [Edited by - Enselic on March 1, 2006 3:37:46 PM]

Share on other sites
One way might be to force the threads to check a boolean array (one element per thread) which, when TRUE, keeps the thread going. A user cancelling would set the appropriate value to FALSE getting the thread to complete its task when it next checks the array.

I'm not sure getting the threads to check a sync block like this is good practise, but it works for me. (Remember to mark the array with the volatile modifier so that changes get written immediately back to it).

Phillip Hamlyn

Share on other sites

The difficulty lies in the fact that once the thread starts to handle data, it does that by recursivly calling methods.

The method that gets called recursivly goes like this
		/// <summary>		/// Gets the childs to the Childs property of the folder. This method is also called on		/// each child.		/// </summary>		public void RetriveChilds()		{			string[] subDirPaths;			try {				subDirPaths = Directory.GetDirectories(FullPath);			} catch (SystemException) {				readAcces = false;				return;			} 			foreach (string subDirPath in subDirPaths) {				FileSystemFolder subDir = new FileSystemFolder(subDirPath, this);								subDir.RetriveChilds();				Childs.Add(subDir);			}			NbrOfFileSystemFolders += subDirPaths.Length;		}

which is a method on the class FileSystemFolder.

What the thread would do when it finds items in the list is to create a root-FileSystemFolder, and then call RetriveChilds() on that node. That will recursivly build the folder tree.

This makes it hard to let the thread look up if it should continue iterating since it 'gets lost' within a lots of RetriveChilds()-calls.

I suspect that I am on the wrong track, i.e. there might be another way of doing the whole thing.

Share on other sites
How big a problem is this?

Maybe I'm just looking at it oddly, but the scenario where a user will change their mind seems slim. Why not just let the thread run through to completion, and toss the results if it's for a file that the user excluded? Seems like less of an impact than making every thread waste time checking to see if the user changed their minds.

Share on other sites
Just one idea...

For each thread that is working on a folder I would just create a variable that contains the folder that it is working on.

If the user removes a folder check if it effects one of your threads by just comparing with that variables. If it has, set a boolean that belongs to that thread to true. But don't set it to false under any conditions from here.

You then have to check in each iteration if the boolean is true, and return if it is. When you proceed to the next folder in your list always reset it to false.

Share on other sites
By coincidence I stumbled across an MSDN article named How to: Create and Terminate Threads which gave some insight in what a solution could look like. The key here is the volatile keyword.

I also found another article worth a read for multithreaders, named How To Stop a Thread in .NET (and Why Thread.Abort is Evil).

Telastyn
You have good point, chances that someone would change their mind are slim. However, I would like to give users a feel of a high quality application, and a HQ app would allow the user to stop a background pocess whenever he/she feels for it.

AP
I will use such a solution with the volatile keyword added.

• 17
• 10
• 19
• 14
• 19