C#跳过一些

时间:2017-03-09 21:19:37

标签: c#

我今天问一个问题,因为我看起来正在跳过某些项目,我认为这是因为它运行得太快而且做得太多,有没有办法解决这个问题而不需要手动设置一个类似10ms的thread.sleep循环元素?

这是一个已知问题吗?任何帮助,将不胜感激!我正在谈论的for循环是第三个,一个在两个嵌套的for&#39>之后的那个

在前2个foreach的跑步后,大约有104个项目,最多可能有1000个,停止跳过的最佳方法是什么?

如果我没有在那里放一个thread.sleep,那么只有第一次出现,foreach DONT中的其他项目才能正常工作

如果我做了10ms,3-4则错过了 如果我做15毫秒他们都工作

List<string> squares = new List<string>();

var minX = 1;
var minY = 1;
var maxX = room.Model.MapSizeX;
var maxY = room.Model.MapSizeY;

for (var currentX = minX; currentX <= maxX; ++currentX)
{
    for (var currentY = minY; currentY <= maxY; ++currentY)
    {
        if (!room.GetGameMap().ItemCanBePlacedHere(currentX, currentY))
        {
            continue;
        }

        squares.Add(currentX + "," + currentY);
    }
}

foreach (string tileString in squares)
{
    var x = Convert.ToInt32(tileString.Split(',')[0]);
    var y = Convert.ToInt32(tileString.Split(',')[1]);

    session.SendWhisper("Attempting to place an item in X" + x + " and Y" + y);

    if (!room.GetGameMap().ItemCanBePlacedHere(x, y))
    {
        session.SendWhisper("Can't place an item in X" + x + " Y" + y);
        continue;
    }

    var newItem = new Item(new Random().Next(), room.RoomId, 1536, "", x, y, 0, 0, session.GetPlayerData().Id, 0, 0, 0, "", room);

    newItem.ExtraData = "1";
    newItem.UpdateState();

    room.SendMessage(new ObjectAddComposer(newItem, room));
    room.GetGameMap().AddToMap(newItem);

    // I only put this here because it skips
    // This slows it down a little and doesnt 
    // Make it skip 3-5 of the items 
    System.Threading.Thread.Sleep(15);
}

0 个答案:

没有答案