Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

while loop not finishing 1

Status
Not open for further replies.

princesszea

Technical User
Aug 11, 2008
33
GB
Hi,

I have a while loop that checks a folder for a file, once the file is moved from the folder for some reason the while loop does not exit as it still thinks the length is still one although there is no file in the folder.

Code:
 while (files.Length > 0)
            {  
               //some code
            }
            
            if (files.Length == 0)
            {
             //some code

             }

Thanks
 
yes, this makes sense. however you haven't provided enough information for us to help you further. if you provide the code in question we should be able to provide a solution.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Hi Jason,

The code is below.


Code:
string[] files = Directory.GetFiles(filelocation, "*.csv", SearchOption.TopDirectoryOnly);


        while (files.Length > 0)
            {  
                System.Threading.Thread.Sleep(int.Parse(10000));
                
            }
            
            if (files.Length == 0)
            {
                System.Threading.Thread.Sleep(int.Parse(1000));

             }

Thanks
 
Directory.GetFiles returns an array of strings. get the files, get the file names, release the references, return the array of files. once called it has nothing to do with the system IO.
Code:
while(true)
{
   var files = Directory.GetFiles(...);
   if(files.Length == 0) 
   {
      break;

   }
   foreach(var file in files)
   {
      Process(file);
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
daddy... excellent point. any time you monitor the file system you should use the file system watcher.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top