0

I was reading this post Limit the number of parallel threads in C# and trying to use it to send multiple files via ftp at the same time:

Perhaps something along the lines of:

    ParallelOptions options = new ParallelOptions();

    options.MaxDegreeOfParallelism = 4;
    Then in your loop something like:

    Parallel.Invoke(options,
     () => new WebClient().Upload("http://www.linqpad.net", "lp.html"),
     () => new WebClient().Upload("http://www.jaoo.dk", "jaoo.html"));

I am trying to add the files in my directory to the Invoke but I wasn't sure how to add them:

  var dirlisting = Directory.GetFiles(zipdir, "*.*", SearchOption.TopDirectoryOnly);
  if (!dirlisting.Any())
  {
    Console.WriteLine("Error! No zipped files found!!");
    return;
  }

  foreach (var s in dirlisting)
  {
    var thread = new Thread(() => FtpFile.SendFile(s));
    thread.Start();
  }

I wasn't sure how to add them to the list of files to send. I only want 3 to go up at a time.

How do I add a thread for each file in the directory listing to be sent?

Community
  • 1
  • 1
ErocM
  • 4,505
  • 24
  • 94
  • 161
  • 2
    `dirlisting.AsParallel().WithDegreeOfParallelism(3).ForAll(FtpFile.SendFile)` , C# is pretty amazing at this sort of thing, you shouldn't worry about threads, although - I'd recommend looking into asynchronous I/O if you want to do it efficiently, namely - with the newer `HttpClient`. – Benjamin Gruenbaum May 26 '14 at 21:32

3 Answers3

0

Something along these lines should do the trick

ParallelOptions o = new ParallelOptions();
o.MaxDegreeOfParallelism = 3;

Parallel.ForEach(dirlisting, o, (f) => 
{
   Ftp.SendFile(f);
});
sondergard
  • 3,184
  • 1
  • 16
  • 25
0

Parallel.For family of APIs is for CPU-bound tasks. You have IO-bound tasks here. Ideally, you should be using some asynchronous I/O Task-based API.

HttpClient doesn't support FTP uploads, but you can use FtpWebRequest.GetRequestStreamAsync and Stream.WriteAsync. You can use WebClient.UploadFileAsync too, but you'd need to create a new WebClient instance per each UploadFileAsync, as WebClient doesn't support multiple operation in parallel on the same instance.

Then you can use TPL Dataflow Library or just SemaphoreSlim to limit the level of parallelism. For examples, check "Throttling asynchronous tasks".

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
0

These lines Worked for me.

var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 10 };
Parallel.ForEach(dirlisting, options, (item) =>
{
   FtpFile.SendFile(item)
});
Prince Prasad
  • 1,528
  • 1
  • 16
  • 20