tags:

views:

330

answers:

2

Hi, I am trying to use

private TcpClient tcpClient;

in a class and every time when I send a command like LIST, RETR, STOR, I used to lock the TCPClient for that particular time of execution of commands and getting response.

Now when I send the other command in between the one is executing, It isn't able to lock that tcpclient instance again.

How should I send the other command when one is executing. For all commands I need to lock the TCPClient, that I can't change.

+1  A: 

This is a design problem. You need to design a multithreaded way of accessing the TcpClient without locking it excessivly

May i suggest you use a queue instead of locking the TcpClient ? I reckon your trying to build an FTP client. (i am guessing based on the LIST, RETR and STOR commands).

An example of a simple queue

private TcpClient client;
private Queue<string> commands = new Queue<string>();
private AutoResetEvent resume = new AutoResetEvent(false);

public void Add(string cmd)
{
  lock(commands) { commands.Enqueue(cmd); }
  resume.Set();

}

private void ThreadRun() // this method runs on a different thread
{
   while(!quit.WaitOne(0,true))
   {
      resume.WaitOne();
      string command;
      lock(commands) { command = commands.Dequeue(); }
      Process(command);

   }


}

You should refrain from trying to "lock" objects just to get it thread-safe. A thread-safe application is designed to operate with minimal locking, rather than forcing locks to enforce thread safety.

Andrew Keith
it means when One command is fully executed then only again second command will execute. That's what I don't want, I want to execute the threads independently, but due to locking of TCPClient, I can't run the different threads.
marshalprince
The problem is not with locking of the TCP client. The problem is that you have a single TCP client that is maintaining a single connection to a server. If you want multiple commands at once, have multiple TCP clients, so you can have multiple conversations going on at once.
Yuliy
A: 

Use one TcpClient for each thread.

Chad Okere
is it a good way opening more than one tcpclient??If the app connects to a data server and also listens to incoming messages over it???
dankyy1