gpt4 book ai didi

c# - 线程 sleep C#

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:13 24 4
gpt4 key购买 nike

我有一个关于线程的问题,我有一个与多个客户端连接的多线程 TCP 请求。线程的操作需要很长时间。 (也许一分钟左右)。如果只有一个线程在运行,我应该如何让我的 sleep 让所有线程拥有相同的时间而不延迟?

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

这是做 sleep 的最佳方式吗?还是我应该给它更长的 sleep 时间?其他线程是否只有 100 毫秒,还是给他们时间开始然后完成?因为我有一种感觉,一个线程完成工作的速度比其他线程快得多...我还能做些什么来确保它们都以相同的优先级完成工作吗?

编辑,更多代码:

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);
}
}
}

如有不明白请提问。

/尼克

最佳答案

应该有一个集中的地方,任务生产者存储新任务,任务消费者从那里获取任务来处理它们。如果所有任务都在消费者开始工作之前生成,那么问题会简单一些。您的工作线程是任务消费者。任务通常用一个对象表示,该对象包含完成任务所需的所有相关数据。工作线程不应该有任何形式的 sleep ,因为哪个线程更快以及它完成了多少任务并不重要。只要有任务要消费,线程就会循环消费任务。此消费受一些保护 lock .

编辑:我建议重组代码,以便只有一个线程以异步方式(BeginReadReadAsync)执行所有读取操作。代码将开始读取每个套接字,并等待某个套接字接收到数据或关闭。如果接收到套接字的数据并且数据已完成,则生成新任务。您必须缓冲不完整的数据并读取更多数据,直到数据完整。当任务产生时,一个任务消费者最终应该拿起任务,处理它并将结果发送到相应的套接字。所以套接字在一个线程中读取并在多个线程中写入。为防止某些竞争条件,如果有未完成的未完成任务产生,主读取器线程不得为特定套接字读取更多数据。任务完成后,主线程可能会开始读取更多数据。

我没有覆盖所有的角落,因为那样会产生一堵文字墙。也许有一个图书馆可以为你做所有的交流,但我不知道。

关于c# - 线程 sleep C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12212226/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com