Using backgroundworker on built in .NET functions

How can I determine the progress of various built-in functions in .NET.  For example if I want to use the backgroundworker for the GetFiles() function.  How do I do it?

Within the Do_work event for backgroundworker, I list the My.Computer.FileSystem.GetFiles() function, which basically gets all the files in a particular folder.  However, I want to get the progress of where the system is within that function (Getfiles()), not within the Do_work event handler.
dbaraschAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
To give a percentage of completion, which is necessary for a progressbar, you FIRST have to know how many items TOTAL are being searched thru and/or returned.

...but the whole point of a search is to find that information in the first place!  Thus this is a chicken and egg problem...

To get a total count we have to recursively visit every folder on the drive (or search path)...something that we don't know how long will take because we only have knowledge of how many files are at the CURRENT level in the folder hierarchy.

The ProgressBar has a Marquee mode, which simply shows that a process is running:
http://msdn.microsoft.com/en-us/library/system.windows.forms.progressbarstyle.aspx

    "Marquee - Indicates progress by continuously scrolling a block across a ProgressBar in a marquee fashion."

This is exactly what the built-in Windows Explorer search does when it wants to report progress during a search.  It doesn't give an exact percentage complete...it just bounces back in forth in "marquee" mode to show that work is being done.
0
 
brawneyCommented:
I don't see how this can be done.  I don't see any Events on the FileSystem or DirectoryInfo classes that would give you any feedback on progress.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you want more control over the process then write your own manual recursive file searching algorithm...
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Wayne Taylor (webtubbs)Commented:
The GetFiles function simply returns a collection of Strings. Do you want to show the progress while it's building that collection, or when you are looping through the file names?
0
 
dbaraschAuthor Commented:
Webtubbs,

Yes.  I want to show the progress while its building that collection.  How can I do that?  I can imagine it being impossible.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
It's a SEALED function so I'm pretty sure you can't get progress WHILE it's collecting the file names...
0
 
dbaraschAuthor Commented:
If you can get the progress, then that really stinks.  If the user wants to get files throughout the whole drive, that could take a long time and I have no way to show the progress of the search.
0
 
brawneyCommented:
If you want to load the whole drive then you should probably do as "Idle Mind" suggested and write your own recursive loop to read in each folder or folder tree and provide progress along the way.  It will be difficult to get an actual percentage progress, but you could report total files or total folders currently loaded this way.

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.