honey the codewitch
honey the codewitch
CC#
Created by honey the codewitch on 12/18/2024 in #help
Looking for a better way to cancel on keypress from the console
I'm using the following code to trigger a cancellation token when the user presses a key. I had to use a thread, because some of these operations are long running, so I can't just check for keypresses between async calls. I feel like there has to be a better way than spawning a thread, but it's just not coming to me.
static void MonitorThreadProc(object state)
{
var cts = (CancellationTokenSource)state;
while(!Console.KeyAvailable)
{
Thread.Sleep(10);
}
cts.Cancel();
}
static async Task<int> Main(string[] args)
{
if (args.Length < 2)
{
return -1;
}
var port = args[0];
var path = args[1];
try
{
using (var link = new EspLink(port))
{
var cts = new CancellationTokenSource();
var mon = new Thread(new ParameterizedThreadStart(MonitorThreadProc));
mon.Start(cts);

var tok = cts.Token;
Console.WriteLine("Press any key to cancel...");
Console.Write("Connecting...");
await Console.Out.FlushAsync();
await link.ConnectAsync(true, 3, true, tok, link.DefaultTimeout, new EspProgress());
Console.WriteLine("\bdone!");
await Console.Out.FlushAsync();
... // complete code is here https://pastebin.com/Cpu18pYm
mon.Abort();
}
return 0;
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation canceled by user. Device may be in invalid state.");
return 1;
}
}
static void MonitorThreadProc(object state)
{
var cts = (CancellationTokenSource)state;
while(!Console.KeyAvailable)
{
Thread.Sleep(10);
}
cts.Cancel();
}
static async Task<int> Main(string[] args)
{
if (args.Length < 2)
{
return -1;
}
var port = args[0];
var path = args[1];
try
{
using (var link = new EspLink(port))
{
var cts = new CancellationTokenSource();
var mon = new Thread(new ParameterizedThreadStart(MonitorThreadProc));
mon.Start(cts);

var tok = cts.Token;
Console.WriteLine("Press any key to cancel...");
Console.Write("Connecting...");
await Console.Out.FlushAsync();
await link.ConnectAsync(true, 3, true, tok, link.DefaultTimeout, new EspProgress());
Console.WriteLine("\bdone!");
await Console.Out.FlushAsync();
... // complete code is here https://pastebin.com/Cpu18pYm
mon.Abort();
}
return 0;
}
catch (OperationCanceledException)
{
Console.WriteLine("Operation canceled by user. Device may be in invalid state.");
return 1;
}
}
That's an excerpt. The whole code is here: https://pastebin.com/Cpu18pYm
45 replies
CC#
Created by honey the codewitch on 11/15/2024 in #help
How to get a Type by string without specifying the assembly name, if it's among loaded asms
Okay, it's been forever since I had to do this in C#, but I need to get a Type by name without having to specify the assembly name, if it is among the currently loaded assemblies for the app domain. What's the best way to do this. Basically I want it to do what Type.GetType() does but a bit more permissive in that it searches all loaded assemblies instead of just the calling assembly when it comes to nonqualified names
3 replies