private void notifyMessage(HMessage message, Action <HMessage> messageDelegate) { // 1 - we search the delegate with the ref if any in delegate dictionnary. if (messageDelegates.Count > 0 && message.GetRef() != null && messageDelegates.ContainsKey(HUtil.GetApiRef(message.GetRef()))) { string msgRef = HUtil.GetApiRef(message.GetRef()); if (timerOutDictionary.ContainsKey(msgRef)) { ThreadPoolTimer timer = timerOutDictionary[msgRef]; timerOutDictionary.Remove(msgRef); if (timer != null) { timer.Cancel(); } } Action <HMessage> action = messageDelegates[msgRef]; messageDelegates.Remove(msgRef); if (action != null) { IAsyncAction threadPoolWorkItem = ThreadPool.RunAsync( (source) => { action(message); } ); } } // 2 - if the ref can not provide a delegate, we try the parameter sent else if (messageDelegate != null) { IAsyncAction threadPoolWorkItem = ThreadPool.RunAsync( (source) => { messageDelegate(message); } ); } else { // 3 - in other case, try the default message delegate onMessage. if (this.onMessage != null) { IAsyncAction threadPoolWorkItem = ThreadPool.RunAsync( (source) => { this.onMessage(message); } ); } } }