bentonstark / starksoft-aspen

.net / mono security and cryptography library that provides client support for ftps, gnupg, smartcard, and socks / http proxies
108 stars 49 forks source link

Multiple Filedownloads #33

Closed mcpat-it closed 6 years ago

mcpat-it commented 6 years ago

Hi,

If I use parralel.for then the download isn't working.

Parallel.ForEach(OrderedList, New ParallelOptions With
                     {
                     .MaxDegreeOfParallelism = Environment.ProcessorCount
                     }, Sub(file)
                                        Dim ms As MemoryStream = New MemoryStream
                                        ftpclient.GetFile(file.fullpath, ms, Starksoft.Aspen.Ftps.FileAction.Create)
                      End Sub)

Regards MCPAt

bentonstark commented 6 years ago

The Parallel class is designed to multi-thread classes that are thread-safe or can be multi-threaded. The FtpClient by design cannot be used this way because most FTP servers require a unique session per client interaction (e.g. unique login and session and only one active operation per session).
https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/potential-pitfalls-in-data-and-task-parallelism

If you want to use the FtpClient in a multi-threaded way you will need to create multiple FtpClient instances each with its own unique session to the FTP server and then call the GetFile() method on each one of those unique instances.

mcpat-it commented 6 years ago

Ok thank you for the answer!