1

in a socket connection between client-server, i'm receiving informations about remote pc in a thread (in a loop) and a other specific info in another thread.

My problem is because when second thread is created the first is stopped immediately, but info that i want receive using second thread is necessary a loop as show below on code.

So, how can execute two thread simultaneously without stop some thread?

    // Entry point of first thread

    procedure TSock_Thread2.Execute;
     var
     s: String;

    begin
      inherited;

      while not Terminated and Socket.Connected do
      begin
        if Socket.ReceiveLength > 0 then
        begin
          s := Socket.ReceiveText;

          if Pos('<|CUR|>', s)>0 then begin

          TSTMouse := TSock_Thread5.Create(Socket);
          TSTMouse.Resume;

          Socket.SendText('<|GETCURSORICON|>'); // after this line, TSock_Thread2 stop your execution 

          end;

       end;
    end;



// Entry point of second Thread

    procedure TSock_Thread5.Execute;
    var
    s, Ponteiro: string;

    begin
      inherited;

        while not Terminated and Socket.Connected do
          begin

            if Socket.ReceiveLength > 0 then
            begin
              s := Socket.ReceiveText;

              if Pos('<|MOUSEICON|>', s) > 0 then // receiving mouse icon here
              begin

                Delete(s, 1, Pos('<|MOUSEICON|>', s) + 12);
                Ponteiro := Copy(s, 1, Pos('<|>', s) - 1);

                if Ponteiro <> '' then

                (Form1.LV1.Selected.SubItems.Objects[2] as TForm2).lblPoint.Caption := Ponteiro;

                Sleep(100);
                Socket.SendText('<|GETCURSORICON|>'); // request cursor icon again

              end;

            end;
          end;

        end;

1 Answer 1

2

You have two threads reading from a single socket? That's generally a bad idea from the start. If you really want that, then you can't have them both reading simultaneously because that's not how sockets work.

Any given reader can only read the next byte or sequence of bytes. There's no built-in control to say which bytes are designated for which thread. You need to work out how to synchronize the two threads so they each know when they're allowed to read.

Another idea is to have just one thread reading from the socket. That thread can read sequences of bytes and then dispatch them to other threads for processing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.