Note that there are some explanatory texts on larger screens.

plurals
  1. POThread sleep C#
    primarykey
    data
    text
    <p>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?. </p> <pre><code>while(CanDoSomething) { DoIt(); //Can take all from a few seconds to a few minutes Thread.Sleep(100); } </code></pre> <p>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?</p> <p>EDIT, MORE CODE:</p> <pre><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 &amp;&amp; this.online) { try { if (lastTime.AddMinutes(2) &lt; DateTime.Now) break; data = new byte[1024]; if (ns.DataAvailable &amp;&amp; ns.CanRead) { int recv = ns.Read(data, 0, data.Length); if (recv &gt; 0) { lastTime = DateTime.Now; if ((byte)data[recv - 1] == (byte)255) { int cnt = -1; for (int i = 0; i &lt; 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 &lt; 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' &amp;&amp; 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 &amp;&amp; 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 &gt; 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); } } } </code></pre> <p>Please ask questions if its unclear.</p> <p>/Nick</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload