C# 在输入仍挂起时运行输出
这就是我想做的:C# 在输入仍挂起时运行输出,c#,input,console,output,C#,Input,Console,Output,这就是我想做的: 控制台请求输入 控制台输出东西 当输出到来时,仍然可以随时输入 因此,它基本上做到了以下几点: string message; Console.WriteLine("Please enter your message"); message = Console.ReadLine(); Console.WriteLine("While you're writing that, this is shown,"); Console.WriteLine("And you can cont
string message;
Console.WriteLine("Please enter your message");
message = Console.ReadLine();
Console.WriteLine("While you're writing that, this is shown,");
Console.WriteLine("And you can continue to write as things are being outputted");
如何实现此功能?我需要运行多个线程吗?将输入从一个控制台窗口发送到另一个控制台窗口如何?下面是一个非常简单的示例,使用:
这是一个无止境的循环,需要一个数字。如果您输入一个数字,程序将开始倒计时,但您可以在输出运行时输入下一个数字。是,这需要第二个线程。我还不知道你说的“从一个控制台窗口到另一个控制台窗口”是什么意思-你是说一个完全不同的windows进程吗?是的,但如果我可以在输入的同时输入,也可以。我真的不需要第二个控制台作为输入框。。。它可能仍然需要多个线程。我甚至不确定是否可以有多个控制台,但是,是的:只需将写入操作放入
System.Threading.Task
中,然后在调用Console.ReadLine()之前启动该任务。
while (true)
{
Console.WriteLine("Enter number");
var input = Console.ReadLine();
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
int n;
if(Int32.TryParse(input, out n))
for(int i=n; i>0; i--)
{
Console.WriteLine(String.Format("{0} -> {1}", n, i));
Thread.Sleep(500);
}
});
}