仅在上次使用后的X秒数后执行方法

时间:2012-12-18 09:18:20

标签: c# timer foreach wait

我有一个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 
                        }
                    }
                }
            }

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

2 个答案:

答案 0 :(得分:2)

简单的时间比较怎么样?

var lastExecution = DateTime.Now;

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

您可以在商品舱中保存“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
}

答案 1 :(得分: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);
                        }
                    }
                }
            }
        }