Advertisement
pashalvov

async route

Jun 21st, 2023
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using static System.Console;
  7. using ShellProgressBar;
  8. using System.ComponentModel;
  9.  
  10. namespace OTPOPS_NetDiag
  11. {
  12.     public class TraceRoute
  13.     {
  14.         static readonly string traceRouteExeecutblePath = "TRACERT.EXE";
  15.         static int tickCount = 30;
  16.         static int runtimes = 0;
  17.         static ProgressBarOptions options = new ProgressBarOptions
  18.         {
  19.             BackgroundCharacter = '○',
  20.             BackgroundColor = ConsoleColor.DarkBlue,
  21.             ProgressCharacter = '●',
  22.             ProgressBarOnBottom = true,
  23.             ForegroundColor = ConsoleColor.White,
  24.             ShowEstimatedDuration = true,
  25.             EnableTaskBarProgress = true,
  26.             CollapseWhenFinished = true,
  27.         };
  28.  
  29.         static ProgressBar pbar;
  30.         static int currentCursorLeft = CursorLeft;
  31.         static int currentCursorTop = CursorTop;
  32.  
  33.         public static void GetTrace(string hostName)
  34.         {
  35.             string argumentsList = $"-d -4 -w 100 {hostName}";
  36.             ProcessStartInfo processStartInfo = new ProcessStartInfo(traceRouteExeecutblePath, argumentsList)
  37.             {
  38.                 UseShellExecute = false,
  39.                 LoadUserProfile = false,
  40.                 WindowStyle = ProcessWindowStyle.Hidden,
  41.                 StandardOutputEncoding = Encoding.UTF8,
  42.                 RedirectStandardOutput = true
  43.             };
  44.             Process process = new Process();
  45.             process.StartInfo = processStartInfo;
  46.             process.Start();
  47.             WriteLine(process.StandardOutput.ReadToEnd());
  48.             process.WaitForExit();
  49.            
  50.             //return null;
  51.         }
  52.  
  53.         public static void GetTraceAsync(string hostName)
  54.         {
  55.             //* Create your Process
  56.             string argumentsList = $"-d -4 -w 100 {hostName}";
  57.             ProcessStartInfo processStartInfo = new ProcessStartInfo(traceRouteExeecutblePath, argumentsList)
  58.             {
  59.                 UseShellExecute = false,
  60.                 LoadUserProfile = false,
  61.                 WindowStyle = ProcessWindowStyle.Hidden,
  62.                 StandardOutputEncoding = Encoding.UTF8,
  63.                 RedirectStandardOutput = true,
  64.                 RedirectStandardError = true
  65.             };
  66.             Process process = new Process();
  67.             process.StartInfo = processStartInfo;
  68.             //* Set your output and error (asynchronous) handlers
  69.             process.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);
  70.             process.ErrorDataReceived += new DataReceivedEventHandler(OutputHandler);
  71.             //* Start process and handlers
  72.  
  73.             pbar = new ProgressBar(tickCount, "1", options);
  74.             currentCursorLeft = CursorLeft;
  75.             currentCursorTop = CursorTop;
  76.             process.Start();
  77.             process.BeginOutputReadLine();
  78.             process.BeginErrorReadLine();
  79.             process.WaitForExit();
  80.             pbar.Tick(30, "Готово");
  81.             pbar.Dispose();
  82.             SetCursorPosition(currentCursorLeft, currentCursorTop + 3);
  83.         }
  84.  
  85.         static void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
  86.         {
  87.             pbar.Tick();
  88.  
  89.             WriteLine(outLine.Data);          
  90.         }
  91.     }
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement