Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:20:16+00:00 2026-06-10T15:20:16+00:00

I have a question about Threads, I got a multithread TCP accplication that have

  • 0

I have a question about Threads, I got a multithread TCP accplication that have connections with multiple clients. The threads have operations that take quite long. (maybe a minute or so). How should I put my Sleeps to let all Threads have the same amount of time without puting delay if its for example only one thread runing?.

while(CanDoSomething)
{
    DoIt();                  //Can take all from a few seconds to a few minutes
    Thread.Sleep(100);
}

Is this the best way to do the Sleeps? Or should I give it a longer sleep-time? Does the other threads only get 100ms or does it give them the time to start and then finnish? Becouse I have a feeling that one thread get the work done much faster then the others… Is there anything else I can do to ensure they all get it done with the same priority?

EDIT, MORE CODE:

private void ListenForClients()
    {
        try
        {

            this.tcpListener.Start();
            while (true)
            {
                TcpClient client = this.tcpListener.AcceptTcpClient();

                Connection c = new Connection(this.parent);
                connectionCollection.Add(c);
                Thread clientThread = new Thread(new ParameterizedThreadStart(c.HandleClientComm));

                threadCollection.Add(clientThread);
                clientThread.Start(client);
            }
        }
        catch (Exception e)
        {
        }
    }
public Connection()
    {
        this.todo = new ArrayList();
        todoT = new Thread(handleToDo);
        todoT.Start();
    }

 public void HandleClientComm(object client)
    {
        try
        {

            TcpClient server = (TcpClient)client;

            NetworkStream ns = server.GetStream();
            byte[] data = new byte[1024];
            string input, stringData;
            online = true;
            DateTime lastTime = DateTime.Now;

            while (true && this.online)
            {
                try
                {
                    if (lastTime.AddMinutes(2) < DateTime.Now)
                        break;

                    data = new byte[1024];
                    if (ns.DataAvailable && ns.CanRead)
                    {
                        int recv = ns.Read(data, 0, data.Length);
                        if (recv > 0)
                        {
                            lastTime = DateTime.Now;
                            if ((byte)data[recv - 1] == (byte)255)
                            {
                                int cnt = -1;
                                for (int i = 0; i < recv; i++)
                                {
                                    if (data[i] == (byte)254)
                                    {
                                        cnt = i;
                                        break;
                                    }
                                }

                                int nr = recv - cnt - 2;
                                byte[] tmp = new byte[nr];

                                for (int i = 0; i < nr; i++)
                                {
                                    tmp[i] = data[cnt + i + 1];
                                }
                                string crc = Encoding.UTF8.GetString(tmp);
                                stringData = Encoding.UTF8.GetString(data, 0, cnt);

                                MsgStruct msgs = new MsgStruct(stringData);
                                msgs.setCrc(crc);
                                Thread.Sleep(200);

                                addTodo(msgs);
                                if (msgs.getMsg()[0] == 'T' && this.type == 1)
                                    this.parent.cStructHandler.sendAck(msgs, this.ID);
                                Console.WriteLine(todo.Count);

                            }
                        }
                    }
                    if (parent.cStructHandler.gotMsg(this.ID))
                    {
                        MsgStruct tmpCs = parent.cStructHandler.getNextMsg(this.ID);

                        if (tmpCs.getMsg().Length != 0 && ns.CanWrite)
                        {
                            byte[] ba = Encoding.UTF8.GetBytes(tmpCs.getMsg());

                            if (tmpCs.getCrc() == "")
                            {
                                ulong tmp = CRC.calc_crc(ba, ba.Length);
                                tmpCs.setCrc(tmp.ToString("X"));
                            }

                            if (tmpCs.canSendByTimeout())
                            {
                                string crcStr = "?" + tmpCs.getCrc() + "?";
                                byte[] bb = Encoding.UTF8.GetBytes(crcStr);
                                crcStr = Encoding.UTF8.GetString(bb);
                                byte[] fullMsg = new byte[ba.Length + bb.Length];
                                bb[0] = 254;
                                bb[bb.Length - 1] = 255;

                                ba.CopyTo(fullMsg, 0);
                                bb.CopyTo(fullMsg, ba.Length);
                                string s = System.Text.UTF8Encoding.ASCII.GetString(fullMsg);

                                ns.Write(fullMsg, 0, fullMsg.Length);
                                Thread.Sleep(200);
                                if (!tmpCs.isAckNeeded())
                                    parent.cStructHandler.removeNextMsg(this.ID);
                            }
                        }
                    }
                    Thread.Sleep(100);
                }
                catch (Exception e)
                {
                    break;
                }

            }
            ns.Close();
            server.Close();
            dead = true;
        }
        catch (Exception e)
        {
            dead = true;
        }
    }



public void handleToDo()
    {
        try
        {
            int cnt = 0;
            while (true)
            {
                if (todo.Count > 0)
                {
                     //SWITCH CASE FOR DIFFERENT MESSAGE TYPES, DOING TASKS DEPENDING ON WHAT ONES...
                     Thread.Sleep(100);
                } 
                else
                {
                    if (dead)
                    {
                        todoT.Abort();
                        todoT = null;
                        break;
                    }
                }
                Thread.Sleep(200);
           }
      }
}

Please ask questions if its unclear.

/Nick

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:20:17+00:00Added an answer on June 10, 2026 at 3:20 pm

    There should be a centralized place where task producers store new tasks, and from where task consumers take tasks to process them. The problem is a bit simpler if all tasks are produced before consumers begin their work. Your working threads are task consumers. Task is usually represented with one object that contains all relevant data needed to complete the task. Working thread should not have any form of Sleep, because it is not important which thread is faster and how many tasks it completes. Threads consume tasks in a loop as long as there are tasks to be consumed. This consuming is protected by some lock.

    EDIT: I would propose the restructuring of code so that there is only one thread that does all the reading, in asynchronous way (BeginRead or ReadAsync). The code would start reading on each socket, and wait until some socket receives data or closes. If data are received for a socket and data are complete then produce new task. You must buffer incomplete data and read more until data are complete. When task is produced one task consumer should eventually pick up the task, process it and send the result to corresponding socket. So socket is read from in one thread and written to in several threads. To prevent some race conditions main reader thread must not read more data for specific socket if there is outstanding incomplete task produced for it. When task is completed main thread may start reading more data.

    I haven’t covered all the corners, because that would produce a wall of text. Maybe there is a library that does all the communication for you, but I don’t know any.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a question regarding multiple-thread method invocation in Java. Let's say we have
I have another question. Quite similar to the other that i already asked (and
I have a question about Java Threads. In java, while running on top of
I have some question about using queues and threads. I already made researches, but
I got a question about joinable threads in wxWidgets. When the user wants it,
I have a question about the 'Event Dispatch Thread'. I have a Main class
I am working on a multi-thread program, and have a question about where to
I have question about parsing in Html helper : I have sth like: @foreach
I have question about clean thory in Python. When: @decorator_func def func(bla, alba): pass
I have question about XSLT1.0. The task is to write out in HTML all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.