线程睡眠C#
本文关键字:线程 | 更新日期: 2025-02-19 12:03:19
我有一个关于线程的问题,我得到了一个与多个客户端连接的多线程TCP应用程序。线程的操作需要相当长的时间。(也许一分钟左右)。例如,如果只有一个线程在运行,我应该如何让我的Sleeps让所有线程都有相同的时间而不延迟?。
while(CanDoSomething)
{
DoIt(); //Can take all from a few seconds to a few minutes
Thread.Sleep(100);
}
这是睡觉的最佳方式吗?或者我应该给它更长的睡眠时间?其他线程只得到100ms,还是给了它们开始然后变finnish的时间?因为我有一种感觉,一个线程完成工作的速度比其他线程快得多。。。我还能做些什么来确保他们都以同样的优先级完成任务吗?
编辑,更多代码:
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);
}
}
}
如果不清楚,请提问。
/Nick
应该有一个集中的地方,任务生产者可以在那里存储新任务,任务消费者可以从那里接收任务来处理它们。如果所有任务都是在消费者开始工作之前生成的,那么问题会简单一些。您的工作线程是任务使用者。任务通常用一个对象表示,该对象包含完成任务所需的所有相关数据。工作线程不应该有任何形式的睡眠,因为哪个线程更快以及它完成了多少任务并不重要。只要有任务要消耗,线程就会消耗循环中的任务。这种消费受到某种锁的保护。
EDIT:我建议对代码进行重组,以便只有一个线程以异步方式(BeginRead或ReadAsync)完成所有读取。代码将开始读取每个套接字,并等待某个套接字接收到数据或关闭。如果接收到套接字的数据并且数据已完成,则生成新任务。您必须缓冲不完整的数据,并读取更多数据,直到数据完成。当任务生成时,一个任务使用者最终应该拿起任务,对其进行处理,并将结果发送到相应的套接字。因此,套接字在一个线程中读取,并在多个线程中写入。为了防止某些竞争条件,如果为特定套接字生成了未完成的未完成任务,则主读取器线程不得读取更多数据。当任务完成时,主线程可能会开始读取更多的数据。
我没有把所有的角落都盖住,因为那样会形成一堵文本墙。也许有一个图书馆为你做所有的交流,但我不知道。
您的操作系统有一个调度程序。它已经在努力给每个线程相等的时间,并且更好地了解线程何时在等待某个东西(因此只会浪费给它的时间)。
我不确定你更深层次的问题是什么,但我确信这不是解决方案。有非常少数的真实代码案例中Sleep()
是有用的(我说"真实代码"是因为在其他一些案例中,它可以帮助调查和测试代码,而不是生产)。无论如何,这些极少数的情况中的大多数已经在库代码中为我们提供了。它们中没有一个需要用高于10
的值进行调用,也很少有一个需要比0
高的值。
这不是那种情况。
您不应该像这样使用任何Sleeps和while循环。即使有while循环,在你的情况下也没有睡眠的用处。
在不知道你的要求的情况下,我建议你看看计时器。