Example #1
0
        /// <summary>
        /// This loop enables to eventually output
        /// BOTH the relevant string AND the ping result
        /// </summary>
        /// <returns>The last task to wait for (Notice: NO `return`)
        /// </returns>
        private static async Task _inputLoopAsync()
        {
            string aLine = "";
            Task   tOut  = null;

            while (true)
            {
                aLine = Console.ReadLine();//blocks
                if (aLine == null)
                {
                    break;//<-> ctrl+Z+Enter
                }
                Task <GYPingResult> tPing = GYPinger.PingAsync(aLine);
                tOut = Task.Run(
                    //`Action` via thread pool:
                    async() =>
                {
                    //`await` is not blocking since via a thread:
                    _setTaskToOutput(tPing.Id, await tPing);
                });
                //tOut.Id != tPing.Id
                //`tPing` is always being awaited, while tOut not yet...
                _map.Add(tPing.Id, tOut);//NOTICE: ID of inner pinging task
            }
            //notice: THE last pending output may have completed,
            //while others are still pending -
            //i.e: the last ping is successful, while others are not
            await tOut;//this method is `async`...

            //LINQ to array: wait all pendingz at `_setTaskToOutput`
            Task.WaitAll(_map.Values.ToArray());
        }
Example #2
0
        private static void _inputLoopEvent()
        {
            var    dlgt  = new PingCompletedEventHandler(_OnPingCompleted);
            string aLine = null;

            while (true)
            {
                aLine = Console.ReadLine();//blocks
                if (aLine == null)
                {
                    break;//<-> ctrl+Z+Enter
                }
                GYPinger.PingWithEvent(aLine, dlgt);
            }
        }