2013-10-15 183 views
0

下面是代碼示例,我希望隊列中的組件採取局部變量「實體」,我該如何執行此操作? THX如何將變量傳遞給Action?

private void DoComparison(StuffEntity entity) 
    { 
     try 
     { 
      bool dataFlag = CheckIsNewData(entity.PickingTime, entity.WarningPeriod); 
      if (dataFlag) 
      { 
       Queue<Action<StuffEntity>> queue = new Queue<Action<StuffEntity>>(); 
       //How can I let the queue stuff take the entity? 
       queue.Enqueue(DelaySendingMessageOut); 
       if (!QueueItem.ContainsKey(entity.FridgeID)) 
       { 
        QueueItem.Add(entity.FridgeID, queue); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      CommonUnity.WriteLog(ex.Message); 
      CommonUnity.WriteLog(ex.StackTrace); 
     } 
    } 

    private void DelaySendingMessageOut(StuffEntity entity) 
    { 
     int pendingPeroid = entity.PendingTime.ToInt(); 
     if (pendingPeroid <= 0) 
      pendingPeroid = 5; 

     Thread.Sleep(pendingPeroid * 60 * 1000); 
     TriggerCheckingBeforeSendMessageOut(entity); 
    } 
+0

目前尚不清楚什麼是你與隊列在做什麼?您已將該委託添加到隊列中,您打算如何調用它? –

+0

'Action'將用於延遲調用該方法。所以你只會在需要調用的時候傳遞實例。如果你需要映射關係。除了'Queue'之外,您需要自定義集合來保存要傳遞的'Action'和'parameter' –

回答

0
queue.Enqueue((e) => DelaySendingMessageOut(entity)); 

但是你可以使用Queue<Action>,因爲你不會使用參數e

Queue<Action> queue = new Queue<Action>(); 
queue.Enqueue(() => DelaySendingMessageOut(entity));