1

I used a component for socket connection in C#

I connect 10 tcp object to server by threading. and I get the recived data in an Event (Data_OnRecived)

here is my code:

// Connect Function

    private void Connect(object State)
    {
        Tcp tcp = (Tcp)State;
        TcpSession tcpSession = new TcpSession();

        tcpSession.RemoteEndPoint = new Dart.Sockets.IPEndPoint(IP, Convert.ToInt32(Port));
        tcpSession.ConnectTimeout = 1;

        try
        {
            tcp.Connect(tcpSession);
        }

        catch(System.Net.Sockets.SocketException ex) {
            tcp.Marshal(ex);
        }
    }

// Initial TCP component

    private void TcpInitialize()
    {
        for (int i = 0; i < MaxConnection; i++)
        {
            SocketControl[i].TCPConnection = new Tcp();

            SocketControl[i].TCPConnection.Data += new System.EventHandler<Dart.Sockets.DataEventArgs>(this.tcp_Data);

            SocketControl[i].TCPConnection.Start(Connect, SocketControl[i].TCPConnection);

            SocketControl[i].TCPConnection.Tag = i;
        }
    }

and TCP_Data Event handler that fire when data recived:

void tcp_Data(object sender, Dart.Sockets.DataEventArgs e)
        {
             TCP tcp = (TCP) Sender;
             int GlobalVariable = tcp.Tag;
        }

my problem is here that tcp_Data will run in threading mode, not in Queque and one by one. can I change it that this event dont start in threading mode?? I mean every data that recived, fire the tcp_Data after another one.

Thanks for any helping

Elahe
  • 1,350
  • 2
  • 17
  • 33

1 Answers1

1

You want to execute only one tcp_Data callback at a time. Make tcp_Data perform this synchronization. For example, it could take a lock:

    object lockObj = new object();
    void tcp_Data(object sender, Dart.Sockets.DataEventArgs e)
    {
         lock (lockObj) {
          TCP tcp = (TCP) Sender;
          int GlobalVariable = tcp.Tag;
          //...
         }
    }

You also could queue this work to a TaskScheduler that is single-threaded.

usr
  • 162,013
  • 33
  • 219
  • 345
  • @urs it didnt change any thing. when I debug this code, I can see that is execute in threading mode – Elahe Oct 12 '14 at 09:03
  • What do you mean by "threading mode"? And what does your statement have to do with this answer? – usr Oct 12 '14 at 09:17
  • And I provided you a solution for that. What do you think about it? – usr Oct 12 '14 at 10:26
  • now there are more thatn thread at time in tcp_Data event handler with your code. @urs can I chat with you about this problem? I want to explain it clearly... – Elahe Oct 12 '14 at 10:31
  • There will be more than one thread in tcp_Data, but only one in the locked region. Do you really observe more than one thread at a time in the locked region? If yes, then the lock object is probably not the same each time. Make lockObj static for a test. – usr Oct 12 '14 at 10:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/62914/discussion-between-elahe-and-usr). – Elahe Oct 12 '14 at 10:42
  • I make lockObj readOnly and my problem solved. but why in this question (http://stackoverflow.com/questions/9621438/confusion-about-the-lock-statement-in-c-sharp) : (1) Does the lock statement ensure that no more than one thread is in the body of the lock statement at any one time? The answer is No?! – Elahe Oct 12 '14 at 11:00
  • 1
    The lock body is exclusive only for a specific lock object instance. If you use multiple instances then your code runs exclusively *per lock object instance*. That's what the lock object argument is *for*. – usr Oct 12 '14 at 11:32