C# 你可以有多个 .net 控制台吗(如 Console.Writeline 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/671163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can you have multiple .net Consoles (as in Console.Writeline)
提问by NormD
It would be handy during debugging to have multiple consoles (not multiple monitors, which I already have). I am imagining something like Console.WriteLine(1, String1) which would send String1 to one console window 1 and Console.WriteLine(2, String2) which would send String2 to console window 2.
在调试期间拥有多个控制台(而不是我已经拥有的多个显示器)会很方便。我正在想象像 Console.WriteLine(1, String1) 这样的东西,它会将 String1 发送到一个控制台窗口 1,而 Console.WriteLine(2, String2) 会将 String2 发送到控制台窗口 2。
采纳答案by Logan Capaldo
Nope
不
A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, then it can call AllocConsole to create a new console or AttachConsole to attach to another console.
一个进程只能与一个控制台相关联,因此如果调用进程已经有一个控制台,则 AllocConsole 函数将失败。一个进程可以使用 FreeConsole 函数将自己与当前控制台分离,然后它可以调用 AllocConsole 来创建一个新的控制台或 AttachConsole 来附加到另一个控制台。
Instead you can use WinForms or WPF to create multiple "console" windows and write a simple WriteLine
method to append text to a text box or similar.
相反,您可以使用 WinForms 或 WPF 创建多个“控制台”窗口并编写一个简单的WriteLine
方法来将文本附加到文本框或类似内容。
Another option is to open a log file, and use that as your second "console".
另一种选择是打开一个日志文件,并将其用作您的第二个“控制台”。
回答by JaredPar
Is it possible to have 2 Windows Consoles in the same application?
是否可以在同一个应用程序中拥有 2 个 Windows 控制台?
No. The Console class is just a very thin wrapper around the Win32 idea of a console. Essentially everything you see when you use
cmd.exe
. There is no way to create 2 of these in a single process.
不。Console 类只是围绕 Win32 控制台理念的一个非常薄的包装器。基本上你在使用时看到的一切
cmd.exe
。无法在单个过程中创建其中 2 个。
Could I have 2 Console-like windows?
我可以有 2 个类似控制台的窗口吗?
Yes. It would be possible to essentially build a very console like class in WPF or WinForms. Use that to spit out text to a command line like application.
是的。基本上可以在 WPF 或 WinForms 中构建一个非常类似于控制台的类。使用它来将文本吐出到命令行之类的应用程序。
回答by tvanfosson
Rather than multiple consoles you might want to consider the idea of multiple log appenders. I'm not a real big fan of printf debugging
, but I do sometimes see a place for multiple log files. Using something like log4net, you can configure multiple appenders and loggers that correspond to different classes/namespaces and log these to separate files. You could use the logging information to help debug using something like TextPadto view the log file(s) while debugging, since TextPad, unlike NotePad, detects and allows you to view the contents of any updates done while the file is open. There may be other editors that allow this, too, but I'm familiar with TextPad.
您可能想要考虑多个日志附加程序的想法,而不是多个控制台。我不是 的忠实粉丝printf debugging
,但有时我确实会看到一个存放多个日志文件的地方。使用类似log4net 的东西,您可以配置对应于不同类/命名空间的多个 appender 和记录器,并将它们记录到单独的文件中。您可以使用日志信息来帮助调试,使用诸如TextPad 之类的东西在调试时查看日志文件,因为 TextPad 与 NotePad 不同,它检测并允许您在文件打开时查看任何更新的内容。可能还有其他编辑器允许这样做,但我对 TextPad 很熟悉。
回答by Pedro Sim?es
I had the same need and I found your question here in SO. A lot of years later :)
我有同样的需求,我在 SO 中找到了你的问题。很多年后:)
In fact, .NET doesn't have this capability, but we can simulate it by splitting the screen with a grid, using some buffering.
事实上,.NET 没有这种能力,但我们可以通过用网格分割屏幕,使用一些缓冲来模拟它。
Here is an example:
下面是一个例子:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void print(int x, int y, int n)
{
for(int i = 0; i < 20; i++)
{
Console.CursorLeft = x; Console.CursorTop = y++;
Console.Write(new string(n.ToString().Last(), 60));
}
}
static void Main(string[] args)
{
var l = new List<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
while (true)
{
Console.Clear();
Console.CursorVisible = false;
for (int i = 0; i < l.Count; i++)
{
print((i % 3) * 60, (i / 3) * 20, l[i]++);
}
Task.Delay(1000).Wait();
}
}
}
}
So the screen is divided into a 3x3 grid and each screen is updated using the cursor position.
所以屏幕被分成一个 3x3 的网格,每个屏幕都使用光标位置更新。
It could be packaged into a lib and a generic API, like you described:
它可以打包成一个库和一个通用 API,就像你描述的那样:
Console.WriteLine(1, String1)
Console.WriteLine(2, String2)
Hope it helps someone.
希望它可以帮助某人。