Advertisement
honey_the_codewitch

C# monitor keypress console

Dec 17th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. static void MonitorThreadProc(object state)
  2. {
  3.     var cts = (CancellationTokenSource)state;
  4.     while(!Console.KeyAvailable)
  5.     {
  6.         Thread.Sleep(10);
  7.     }
  8.     cts.Cancel();
  9. }
  10. static async Task<int> Main(string[] args)
  11. {
  12.     if (args.Length < 2)
  13.     {
  14.         return -1;
  15.     }
  16.     var port = args[0];
  17.     var path = args[1];
  18.     try
  19.     {
  20.         using (var link = new EspLink(port))
  21.         {
  22.             var cts = new CancellationTokenSource();
  23.             var mon = new Thread(new ParameterizedThreadStart(MonitorThreadProc));
  24.             mon.Start(cts);
  25.  
  26.             var tok = cts.Token;
  27.             Console.WriteLine("Press any key to cancel...");
  28.             Console.Write("Connecting...");
  29.             await Console.Out.FlushAsync();
  30.             await link.ConnectAsync(true, 3, true, tok, link.DefaultTimeout, new EspProgress());
  31.             Console.WriteLine("\bdone!");
  32.             await Console.Out.FlushAsync();
  33.             Console.WriteLine("Running stub... ");
  34.             await Console.Out.FlushAsync();
  35.             await link.RunStubAsync(tok, link.DefaultTimeout, new EspProgress());
  36.             Console.WriteLine();
  37.             await Console.Out.FlushAsync();
  38.             await link.SetBaudRateAsync(115200, 115200 * 4, tok, link.DefaultTimeout);
  39.             Console.WriteLine($"Changed baud rate to {link.BaudRate}");
  40.             Console.WriteLine("Flashing... ");
  41.             await Console.Out.FlushAsync();
  42.             using (var stm = File.Open(path, FileMode.Open, FileAccess.Read))
  43.             {
  44.                 await link.FlashAsync(tok, stm, 16*1024, 0x10000, 3, false, link.DefaultTimeout, new EspProgress());
  45.                 Console.WriteLine();
  46.                 Console.WriteLine("Hard resetting");
  47.                 await Console.Out.FlushAsync();
  48.                 link.Reset();
  49.             }
  50.             mon.Abort();
  51.         }
  52.         return 0;
  53.     }
  54.     catch (OperationCanceledException)
  55.     {
  56.         Console.WriteLine("Operation canceled by user. Device may be in invalid state.");
  57.         return 1;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement