Async Directory Traverse Problem

B

Berk

Hello,

I have a asyncronuos directory traverser program. My problem is, after
changing the hard drive it traverses, it keeps traversing the previous
drive, not the current drive. I'm putting here the source code for an
advice, probably needs a thread synhronization:

public delegate void WalkDirectoryProc(string directory);
public System.IAsyncResult BeginWalkDirectory(string directory,
AsyncCallback callback)
{
WalkDirectoryProc walkDirCallback = new
WalkDirectoryProc(WalkDirectory);
return walkDirCallback.BeginInvoke(directory, callback, null);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void WalkDirectory(DirectoryInfo directoryInfo)
{
FileSystemInfo[] fsis =
directoryInfo.GetFileSystemInfos();

try
{
foreach (FileInfo file in directoryInfo.GetFiles())
{
RaiseFileEvent(file);
}
//}
}
catch (InvalidOperationException ioex)
{
Debug.WriteLine(ioex.Message);
throw ioex;
}
catch (UnauthorizedAccessException)
{

}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}

DirectoryInfo[] subDirectories =
directoryInfo.GetDirectories();

foreach (DirectoryInfo subDirectory in subDirectories)
{
WalkDirectory(subDirectory);
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private void scanDirectory_FileEvent(object sender, FileEventArgs e)
{
if (_bufList.Count >= _bufList.Capacity)
{
for (int i = 0; i < 20 - 1; ++i)
listViewContentList.Invoke(new
CrossThreadHandler(delegate() {

listViewContentList.Items.Add(_bufList.No.ToString()).SubItems.AddRange(new
string[] {
_bufList.Name, _bufList.ParentDirName
}
);
}
));

_bufList.Clear();

}

FilenameHelper helper = new FilenameHelper();
helper.No = _fileCount++;
helper.Name = e.Info.Name;
helper.ParentDirName = e.Info.Directory.Name;

_bufList.Add(helper);
}

Regards.
 
B

Berk

Unfortunately, you have not presented a concise-but-complete code
example that reliably reproduces the problem.

In fact, even the code you posted doesn't appear to be compilable, given
that the async BeginWalkDirectory() method takes a string as input, but
then apparently attempts to pass that to a method that takes a
DirectoryInfo instance as input.  The line of code where you instantiate
the delegate to invoke should not even compile, at least not given the
code you've posted.

But, given that the "directory traverser" code that you posted does not
appear to deal with the drive letter at all, but rather just starts with
the path passed to it, there doesn't appear to be any opportunity for it
to get the drive letter wrong, whether as a consequence of improper
thread synchronization or not.

Post a concise-but-complete code example that reliably reproduces the
problem.

Pete


Pete,

Sorry for the unsuitable code I pasted. Next time, I'll be more
careful about pasting a compilable source.
So, it's not suiting to talk about the mistake I did in the absence of
a compilable source code, i can say it was an error when I buffer the
return values. I removed it and my problem is solved.

If it's needed for the complete of topic, i can ensure (paste here) a
complete compilable source code about traversing directories in a
async manner.

Regards.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top