I'm currently working on a hobby project involving a console application. In my program, I fetch data, transform it and eventually it gets parsed to json
. The rough structure looks like this:
static void Main(string[] args)
{
var sw = new Stopwatch();
var worker = new Worker();
var start = DateTime.Now;
var workSpeed = 0.0;
Console.WriteLine($"Initialisation started at {start}");
sw.Start();
var materials = worker.GatherMaterials();
var workStart = DateTime.Now;
Console.WriteLine($"Work started at {workStart}.");
var products = new List<Product>();
foreach (var material in materials)
{
try
{
var product = worker.Process(material);
products.Add(product);
sw.Stop();
workSpeed = (double)products.Count / sw.Elapsed.TotalSeconds;
sw.Start();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
break;
}
}
sw.Stop();
Console.WriteLine();
var finish = DateTime.Now;
Console.WriteLine($"Work finished at {finish}");
Console.WriteLine($"Ran from {start} to {finish} for approximately {sw.Elapsed} ms.");
Console.WriteLine($"Processed {products.Count} products for an average processing speed of {workSpeed} products/s.");
var jsonOptions = new JsonSerializerOptions()
{
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
WriteIndented = true
};
var json = JsonSerializer.Serialize(products, jsonOptions);
File.WriteAllText("products.json", json, Encoding.UTF8);
Console.WriteLine("Saved all products to products.json");
}
There's a bit of code inside the try
block where I temporarily stop the stopwatch to update the processing speed. I did this so that I could put a break point there to check the processing speed. However, I don't want to always run this application in debug mode. Ideally, I'd like to get rid of the part where I stop and resume the stopwatch and instead to have a way to interact with the variables directly through the console.
For example, would it be possible to have a persistent question in the terminal that says something like Press S to get the current processing speed
, so that the value of the workSpeed
variable shows up whenever I press S?