views:

90

answers:

3

Hi, currently I am using lock for sending\receiving the file from FTP through TCPClient class.

But this supports only single file to send and receive.

So what's the better way to use in this situation (lock, mutex, interlock, read\write lock, semaphore).

A: 

Im not sure your situation can be solved by any of those locking contexts. You might want to look into Asynchronous I/O and its various implementations, but specifically in regards to the WebRequest and Webresponse classes.

You did not say whether you were using the default classes for handling your FTP or a custom library. I (perhaps incorrectly) assume you are creating the FTP handlers yourself. If you are using a third party library, you should check to see if they support Async IO, or multithreading in another form.

GrayWizardx
A: 

You can use semaphore it is a classic example to create a number of concurrent works.
http://www.dijksterhuis.org/using-semaphores-in-c/

Sergey Mirvoda
+1  A: 

Let's say you have a list of files you want to transfer in parallel (one thread for each file) and want to wait until all the uploads are finished, you could create an array of WaitHandles (number of wait handles == number of files == number of threads) and pass each thread one of them (wait handle : thread == 1:1) along with the file the thread should transfer.

The thread would transfer the file and before it exists, it would set the wait handle.

In the "main thread" you could wait for all the wait handles from the array to be set using WaitHandle.WaitAll(WaitHandle[]) (I may not be correct about the exact syntax here, but a command like this exists, I'm sure. I've used it before).

This is called a barrier.

Thorsten Dittmar