在运行时获取变量的值(控制台)
Get the value of a variable during runtime (Console)
我目前正在从事一个涉及控制台应用程序的业余爱好项目。在我的程序中,我获取数据,对其进行转换并最终将其解析为 json
。粗略的结构如下所示:
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");
}
try
块中有一些代码,我暂时停止秒表以更新处理速度。我这样做是为了在那里放置一个断点来检查处理速度。但是,我不想总是 运行 这个应用程序处于调试模式。理想情况下,我想摆脱我停止和恢复秒表的部分,而是通过控制台直接与变量交互。
例如,是否有可能在终端中出现类似 Press S to get the current processing speed
的持续问题,以便每当我按 S 时显示 workSpeed
变量的值?
您可以检查 Console.KeyAvailable
的值以查看用户是否按下了某个键(如果按下则为 true
),然后使用 Console.ReadKey(true)
捕获该键他们按下(true
表示不会显示)。
例如:
var sw = new Stopwatch();
Console.WriteLine("Press 'S' to display the elapsed time, 'Q' to quit...");
sw.Start();
while (true)
{
if (Console.KeyAvailable)
{
var input = Console.ReadKey(true).Key;
if (input == ConsoleKey.S)
{
Console.WriteLine($"Elapsed time: {sw.Elapsed.ToString("c")}");
}
else if (input == ConsoleKey.Q)
{
break;
}
}
}
sw.Stop();
Console.WriteLine($"All done. Total elapsed time: {sw.Elapsed.ToString("c")}");
Console.Write("Press any key to exit...");
Console.ReadKey(true);
我认为最好的选择是采用多线程方法:将您的实际逻辑放在单独的线程中,并让主线程处理您感兴趣的查询。请注意,使用多线程方法时,您将需要在两个线程之间共享资源(并最终进行一些同步),以获得您感兴趣的速率。
我目前正在从事一个涉及控制台应用程序的业余爱好项目。在我的程序中,我获取数据,对其进行转换并最终将其解析为 json
。粗略的结构如下所示:
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");
}
try
块中有一些代码,我暂时停止秒表以更新处理速度。我这样做是为了在那里放置一个断点来检查处理速度。但是,我不想总是 运行 这个应用程序处于调试模式。理想情况下,我想摆脱我停止和恢复秒表的部分,而是通过控制台直接与变量交互。
例如,是否有可能在终端中出现类似 Press S to get the current processing speed
的持续问题,以便每当我按 S 时显示 workSpeed
变量的值?
您可以检查 Console.KeyAvailable
的值以查看用户是否按下了某个键(如果按下则为 true
),然后使用 Console.ReadKey(true)
捕获该键他们按下(true
表示不会显示)。
例如:
var sw = new Stopwatch();
Console.WriteLine("Press 'S' to display the elapsed time, 'Q' to quit...");
sw.Start();
while (true)
{
if (Console.KeyAvailable)
{
var input = Console.ReadKey(true).Key;
if (input == ConsoleKey.S)
{
Console.WriteLine($"Elapsed time: {sw.Elapsed.ToString("c")}");
}
else if (input == ConsoleKey.Q)
{
break;
}
}
}
sw.Stop();
Console.WriteLine($"All done. Total elapsed time: {sw.Elapsed.ToString("c")}");
Console.Write("Press any key to exit...");
Console.ReadKey(true);
我认为最好的选择是采用多线程方法:将您的实际逻辑放在单独的线程中,并让主线程处理您感兴趣的查询。请注意,使用多线程方法时,您将需要在两个线程之间共享资源(并最终进行一些同步),以获得您感兴趣的速率。