所以我得到了一个服务器,它可以在while循环中启动下面的任务,它作为我的客户端侦听器运行。这个问题似乎是想通过客户端尽可能快地循环,这是很好的!但是,在完成前一个任务之前,它会以太快的速度启动任务--新的任务会发生(对于同一个客户机)。
我不想让它等到任务完成!我希望它能拍摄更多的任务,但不要再为已经有任务的特定客户端创建任务。
做这件事最好的办法是..。我看到很多人都在使用WhenAll之类的东西,但我并不关心所有的任务。
//the below is in a while loop which goes through the clients that are connected.
if (stream.DataAvailable)
{
// the below task is the one that is firing before the pervious fired one completes.
Task DataInterpetTask = Task.Factory.StartNew(() =>
{
int totalBuffer, totalRecieved = 0;
byte[] totalBufferByte = new byte[4];
byte[] buffer = new byte[0];
byte[] tbuffer;
int rLength, prevLength;
stream.Read(totalBufferByte, 0, 4);
totalBuffer = BitConverter.ToInt32(totalBufferByte, 0);
Console.WriteLine("got stuff: " + totalBuffer);
byte[] buf = new byte[c.getClientSocket().ReceiveBufferSize];
while (totalBuffer > totalRecieved)
{
rLength = stream.Read(buf, 0, buf.Length);
totalRecieved = rLength + totalRecieved;
if (rLength < buf.Length)
{
byte[] temp = new byte[rLength];
Array.Copy(buf, temp, rLength);
buf = temp;
}
prevLength = buffer.Length;
tbuffer = buffer;
buffer = new byte[buffer.Length + rLength];
Array.Copy(tbuffer, buffer, tbuffer.Length);
buf.CopyTo(buffer, prevLength);
}
String msg = Encoding.ASCII.GetString(buffer);
if (msg.Contains("PNG") || msg.Contains("RBG") || msg.Contains("HDR"))
{
Console.WriteLine("Receiving Picture");
RowContainer tempContainer;
if ((tempContainer = MainWindow.mainWindow.RowExists(c)) != null)
{
tempContainer.Image = buffer;
Console.WriteLine("Updating row: " + tempContainer.rowNumber);
MainWindow.mainWindowDispacter.BeginInvoke(new Action(() =>
MainWindow.mainWindow.UpdateRowContainer(tempContainer, 0)));
}
else
{
Console.WriteLine("Adding row for Picture");
MainWindow.mainWindowDispacter.BeginInvoke(new Action(() =>
MainWindow.mainWindow.CreateClientRowContainer(c, buffer)));
}
return;
}
String switchString = msg.Substring(0, 4);
if (msg.Length > 4)
msg = msg.Substring(4);
MainWindow.mainWindowDispacter.BeginInvoke(new Action(() =>
{
if (MainWindow.debugWindow != null)
MainWindow.debugWindow.LogTextBox.AppendText("Received message " + msg + " from client: " + c.getClientIp() + " as a " + switchString + " type" + Environment.NewLine);
}));
RowContainer tContain = MainWindow.mainWindow.RowExists(c);
if(tContain == null)
return;
switch (switchString)
{
case "pong":
c.RespondedPong();
break;
case "stat":
tContain.SetState(msg);
MainWindow.mainWindow.UpdateRowContainer(tContain, 1);
break;
case "osif":
tContain.Os = msg;
MainWindow.mainWindowDispacter.BeginInvoke(new Action(() =>
MainWindow.mainWindow.UpdateRowContainer(tContain, 2)));
break;
case "idle":
tContain.idle = msg;
MainWindow.mainWindowDispacter.BeginInvoke(new Action(() =>
MainWindow.mainWindow.UpdateRowContainer(tContain, 3)));
break;
}
});
}发布于 2015-01-20 14:29:41
如果您不想在上一个客户端完成之前为给定的客户端启动一个新的操作,那么只需跟踪它。要么将客户端对象移动到某个“进行中”列表,以便它甚至不在您正在循环处理的客户端列表中,要么向客户端对象类添加一个标志,指示操作“正在进行”,以便循环可以忽略客户端,直到当前操作完成为止(例如,if (c.OperationInProgress) continue;)。
尽管如此,您似乎正在使用轮询(即检查DataAvailable)来管理I/O --这是一种非常低效率的方法,最重要的是,这是当前问题的根源。如果您使用的是更好的异步模型,那么您的代码将更高效,甚至不会出现这个问题,因为异步API将提供您所需的所有状态管理,以避免对给定客户端的重叠操作。
不幸的是,您的代码示例非常稀疏,缺少了提供关于如何更改实现以使用异步模型的具体建议所需的所有细节(以及有哪些小代码,包含了许多在a good code example中找不到的额外代码)。因此,希望上面的内容足以让您走上正确的解决方案。
https://stackoverflow.com/questions/28036473
复制相似问题