示例#1
0
        private static Task SetupConnections(bool watchMode, bool IPv6Mode, List <string> connections, CancellationTokenSource cancelationSource)
        {
            var tasks            = new List <Task>();
            var requests         = connections.Select(connection => ConnectionRequest.Parse(connection, IPv6Mode));
            var connectors       = requests.Select(r => new ConnectionTester(r)).ToList();
            var printer          = new OutputPrinter(requests);
            var cancelationToken = cancelationSource.Token;

            SetCancelEventHandler(cancelationSource, startLine + connectors.Count, extraSpacing);
            if (watchMode)
            {
                tasks.Add(CheckIfEscPress(cancelationSource));
            }
            printer.PrintTable();
            printer.HideCursor();
            for (int index = 0; index < connectors.Count; index++)
            {
                tasks.Add(
                    Connect(
                        watchMode,
                        watchMode && connectors.Count == 1,
                        connectors[index],
                        Console.CursorTop + index,
                        printer,
                        cancelationToken
                        ));
            }
            return(Task.WhenAll(tasks));
        }
示例#2
0
 private static void FinalCleanup(CancellationTokenSource sourceToken, int endLine, int spacing)
 {
     sourceToken.Cancel();
     OutputPrinter.CleanUpConsole(endLine, spacing);
     sourceToken.Dispose();
 }
示例#3
0
        private static async Task Connect(bool watchMode, bool addLineOnChange, ConnectionTester connection, int index, OutputPrinter printer, CancellationToken cancelationToken)
        {
            await Task.Run(async() =>
            {
                ConnectionResult oldResult = null;
                while (!cancelationToken.IsCancellationRequested)
                {
                    var watch  = System.Diagnostics.Stopwatch.StartNew();
                    var result = connection.TryConnect();
                    if (oldResult == null)
                    {
                        oldResult = result;
                    }
                    if (!oldResult.IsEquivalent(result))
                    {
                        oldResult = result;
                        if (addLineOnChange)
                        {
                            index++;
                        }
                    }
                    printer.PrintLogEntry(oldResult.TimeStamp, result, index);
                    if (!watchMode)
                    {
                        break;
                    }

                    if (watch.ElapsedMilliseconds < 1000)
                    {
                        try
                        {
                            await Task.Delay(1000 - (int)watch.ElapsedMilliseconds, cancelationToken);
                        }
                        catch (Exception) { }
                    }
                }
            });
        }