protected IAsyncResult BeginSendMessageParallel <TMessage>(TMessage message, AsyncCallback requestCallback, object state) where TMessage : DnsMessageBase, new() { List <DnsClientEndpointInfo> endpointInfos = GetEndpointInfos <TMessage>(); DnsClientParallelAsyncState <TMessage> asyncResult = new DnsClientParallelAsyncState <TMessage> { UserCallback = requestCallback, AsyncState = state, Responses = new List <TMessage>(), ResponsesToReceive = endpointInfos.Count }; foreach (var endpointInfo in endpointInfos) { DnsClientParallelState <TMessage> parallelState = new DnsClientParallelState <TMessage> { ParallelMessageAsyncState = asyncResult }; lock (parallelState.Lock) { parallelState.SingleMessageAsyncResult = BeginSendMessage(message, new List <DnsClientEndpointInfo> { endpointInfo }, SendMessageFinished <TMessage>, parallelState); } } return(asyncResult); }
private void SendMessageFinished <TMessage>(IAsyncResult ar) where TMessage : DnsMessageBase, new() { DnsClientParallelState <TMessage> state = (DnsClientParallelState <TMessage>)ar.AsyncState; List <TMessage> responses; lock (state.Lock) { responses = EndSendMessage <TMessage>(state.SingleMessageAsyncResult); } lock (state.ParallelMessageAsyncState.Responses) { state.ParallelMessageAsyncState.Responses.AddRange(responses); state.ParallelMessageAsyncState.ResponsesToReceive--; if (state.ParallelMessageAsyncState.ResponsesToReceive == 0) { state.ParallelMessageAsyncState.SetCompleted(); } } }