4

我有一个 foreach 语句,我需要让它在 foreach 结束时调用一个方法,并且 if 语句仅在其最后执行时间后 3 秒后执行。

这是代码。

  //Go over Array for each id in allItems
  foreach (int id in allItems)
            {
                if (offered > 0 && itemsAdded != (offered * 3) && tDown)
                {
                    List<Inventory.Item> items = Trade.MyInventory.GetItemsByDefindex(id);
                    foreach (Inventory.Item item in items)
                    {
                        if (!Trade.myOfferedItems.ContainsValue(item.Id))
                        {
                            //Code to only execute if x seconds have passed since it last executed.
                            if (Trade.AddItem(item.Id))
                            {
                                itemsAdded++;
                                break;
                            }
                            //end code delay execution 
                        }
                    }
                }
            }

而且我不想让它睡觉,因为当添加一个项目时,我需要从服务器获得该项目已添加的确认。

4

2 回答 2

2

简单的时间比较怎么样?

var lastExecution = DateTime.Now;

if((DateTime.Now - lastExecution).TotalSeconds >= 3)
...

您可以在您的 Trade-Class 中保存“lastExecution”。当然,如果没有经过 3 秒,则不会使用此解决方案调用代码块(不添加项目)。

--//------------------------------------

使用计时器的不同解决方案:我们以编程方式添加 Windows 窗体计时器组件。但是,如果您使用此解决方案,您将在程序员地狱中结束;-)

//declare the timer
private static System.Windows.Forms.Timer tmr = new System.Windows.Forms.Timer();


//adds the event and event handler (=the method that will be called)
//call this line only call once
tmr.Tick += new EventHandler(TimerEventProcessor);

//call the following line once (unless you want to change the time)
tmr.Interval = 3000;    //sets timer to 3000 milliseconds = 3 seconds

//call this line every time you need a 'timeout'
tmr.Start();            //start timer           


//called by timer
private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
  Console.WriteLine("3 seconds elapsed. disabling timer");
  tmr.Stop(); //disable timer
}
于 2012-12-18T09:45:56.693 回答
1
        DateTime? lastCallDate = null;
        foreach (int id in allItems)
        {
            if (offered > 0 && itemsAdded != (offered * 3) && tDown)
            {
                List<Inventory.Item> items = Trade.MyInventory.GetItemsByDefindex(id);
                foreach (Inventory.Item item in items)
                {
                    if (!Trade.myOfferedItems.ContainsValue(item.Id))
                    {
                        //execute if 3 seconds have passed since it last execution...
                        bool wasExecuted = false;
                        while (!wasExecuted)
                        {
                            if (lastCallDate == null || lastCallDate.Value.AddSeconds(3) < DateTime.Now)
                            {
                                lastCallDate = DateTime.Now;
                                if (Trade.AddItem(item.Id))
                                {
                                    itemsAdded++;
                                    break;
                                }
                                wasExecuted = true;
                            }
                            System.Threading.Thread.Sleep(100);
                        }
                    }
                }
            }
        }
于 2012-12-18T14:26:39.603 回答