示例#1
0
        /// <summary>
        /// Internet Control Message Protocol (ICMP) echo message 를 비동기적으로 보냅니다.
        /// </summary>
        private static Task<PingReply> SendTaskCore(Ping ping, object userToken, Action<TaskCompletionSource<PingReply>> sendAsync) {
            ping.ShouldNotBeNull("ping");
            sendAsync.ShouldNotBeNull("sendAsync");

            if(IsDebugEnabled)
                log.Debug("Ping 작업을 비동기적으로 수행하는 Task를 생성합니다...");

            var tcs = new TaskCompletionSource<PingReply>(userToken);
            PingCompletedEventHandler handler = null;
            handler = (sender, e) => EventAsyncPattern.HandleCompletion(tcs, e, () => e.Reply, () => ping.PingCompleted -= handler);
            ping.PingCompleted += handler;

            try {
                sendAsync(tcs);
            }
            catch(Exception ex) {
                ping.PingCompleted -= handler;
                tcs.TrySetException(ex);
            }

            return tcs.Task;
        }