/// <summary> /// Does the work. /// </summary> /// <param name="wKey">The w key.</param> public void doWork(Object wKey) { string workKey = wKey as string; WorkKeyFlag flag = null; lock (_lock) { //A0.01 flag = workKeyFlgDic[workKey]; //Start A0.01 if (!workKeyFlgDic.ContainsKey(workKey)) { logger.Warn(string.Format("workKeyFlgDic ,key:{0} not exist." , workKey)); return; } else { flag = workKeyFlgDic[workKey]; } //End A0.01 } if (flag.CompareAndSet(WorkKeyFlag.UNSET, WorkKeyFlag.SET)) { while (true) { try { BackgroundWorkItem workItem = null; lock (_lock) { if (!workDic.ContainsKey(workKey) || workDic[workKey].Count == 0) { if (workDic.ContainsKey(workKey)) { workDic.Remove(workKey); } if (workKeyFlgDic.ContainsKey(workKey)) { workKeyFlgDic.Remove(workKey); } break; } workItem = workDic[workKey].Dequeue(); } work.doWork(workKey, workItem); } catch (Exception ex) { logger.WarnException("Do Work Exception !", ex); } } flag.CompareAndSet(WorkKeyFlag.SET, WorkKeyFlag.UNSET); } }
/// <summary> /// Triggers the background work. /// </summary> /// <param name="workKey">The work key.</param> /// <param name="workItem">The work item.</param> public void triggerBackgroundWork(string workKey, BackgroundWorkItem workItem) { string wKey = workKey.Trim(); WorkKeyFlag flag = null; lock (_lock) { Queue <BackgroundWorkItem> queue = null; if (!workDic.ContainsKey(wKey)) { queue = new Queue <BackgroundWorkItem>(); workDic.Add(wKey, queue); } else { queue = workDic[wKey]; } if (!workKeyFlgDic.ContainsKey(wKey)) { flag = new WorkKeyFlag(); workKeyFlgDic.Add(wKey, flag); } else { flag = workKeyFlgDic[wKey]; } if (MaxBackgroundQueueCount > 0 && queue.Count >= MaxBackgroundQueueCount) { queue.Dequeue(); logger.Debug("Over Max Background Queue Count[DriverName:{0}][Count:{1}]", DriverName, MaxBackgroundQueueCount); } queue.Enqueue(workItem); //workDic[wKey].Add(workItem); } if (flag.Compare(WorkKeyFlag.UNSET)) { ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(doWork), wKey); //doWork(wKey); } }