Reputation: 11344
I want to process only X number of files at a time for below infinite loop. Below codes give me all files at a time, how to get only X number of files?
while (true)
{
var files = new DirectoryInfo(@"path")
.GetFiles()
.OrderBy(p => p.LastWriteTimeUtc)
.ToList();
foreach (var f in files)
{
//do some processing
Console.WriteLine(f.Name);
f.Delete();
}
Thread.Sleep(5000);
}
Upvotes: 1
Views: 380
Reputation: 16079
You can use combination of Skip()
.Take(X)
which will process your files in batches
Skip() : Bypasses a specified number of elements in a sequence and then returns the remaining elements.
Take() : Returns a specified number of contiguous elements from the start of a sequence.
Process files in batches,
var batchSize = 10; //Decide batch size I considered 10 files in a batch
var skipBatch = 0 //Skip count
while (true)
{
var files = new DirectoryInfo(@"path").GetFiles()
.OrderBy(p => p.LastWriteTimeUtc).ToList(); //store all files
var skipIntervals = skipBatch * batchSize;
//Exit condition from infinity loop
if(skipIntervals > files.Count)
break;
var filesInBatch = files.Skip(skipIntervals).Take(batchSize);
foreach (var f in filesInBatch)
{
//do some processing
Console.WriteLine(f.Name);
f.Delete();
}
Thread.Sleep(5000);
skipBatch++; //Increment skipBatch count by 1 as this batch is processed
}
Upvotes: 10
Reputation: 52280
Just keep a counter. Each time you process a file, increase it by one. When it hits the limit, you've ended the batch.
while (true)
{
var counter = 0;
var files = new DirectoryInfo(@"path").GetFiles()
.OrderBy(p => p.LastWriteTimeUtc).ToList();
foreach (var f in files)
{
//do some processing
Console.WriteLine(f.Name);
f.Delete();
counter++;
if (counter >= batchSize)
{
Thread.Sleep(timeBetweenBatches);
counter = 0;
}
}
Thread.Sleep(5000);
}
Upvotes: 0
Reputation: 49
while (true)
{
var files = new DirectoryInfo(@"path").GetFiles()
.OrderBy(p => p.LastWriteTimeUtc).ToList();
foreach (var f in files)
{
Thread.Sleep(5000);//should be up here
//do some processing
Console.WriteLine(f.Name);
f.Delete();
}
}
The thread.sleep method is not in the right place
you can always use Await Task.Delay() much better than thread.sleep since it makes the whole program freeze up depending on what you are doing with it
Upvotes: 1