Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用线程同时在控制台上读/写_C#_Multithreading_Console_Simulation - Fatal编程技术网

C# 如何使用线程同时在控制台上读/写

C# 如何使用线程同时在控制台上读/写,c#,multithreading,console,simulation,C#,Multithreading,Console,Simulation,我想实现一个运行模拟的C#控制台应用程序。另外,我想给用户一个机会,在控制台上按“+”或“-”来加速/减速模拟的速度 在控制台上写东西时,有没有办法读取控制台?我相信我可以使用多线程来实现这一点,但我不知道该怎么做(我对C#还是新手) 多谢各位 是的,有一种方法可以“同时”读/写。有几种方法可以做到这一点: 使用另一个线程: 首先,启动一个负责向控制台写入的线程 Thread t = new Thread(()=>{RunSimulation();}); t.IsBackground =

我想实现一个运行模拟的C#控制台应用程序。另外,我想给用户一个机会,在控制台上按“+”或“-”来加速/减速模拟的速度

在控制台上写东西时,有没有办法读取控制台?我相信我可以使用多线程来实现这一点,但我不知道该怎么做(我对C#还是新手)


多谢各位

是的,有一种方法可以“同时”读/写。有几种方法可以做到这一点:

使用另一个线程: 首先,启动一个负责向控制台写入的线程

Thread t = new Thread(()=>{RunSimulation();});
t.IsBackground = true;
t.Start();
模拟方法的大致思路如下:

public void RunSimulation()
{
    while(running)
    {
        // Puts the thread to sleep depending on the run speed
        Thread.Sleep(delayTime);
        Console.WriteLine("Write your output to console!");
    }
}
其次,可以让主线程不断轮询用户输入,以便进行调整

string input = string.Empty;

while(input.Equals("x", StringComparison.CurrentCultureIgnoreCase)
{
    input = Console.ReadKey();
    switch(input)
    {
    case "+":
        // speeds up the simulation by decreasing the delayTime
        IncreaseSpeed();
        break;
    case "-":
        // slows down the simulation by decreasing the delayTime
        DecreaseSpeed();
        break;
    default:
        break;
    }
}
使用计时器: 另一种方法是使用[Timer][1]并调整计时器上的回调频率,而不是调整线程上的睡眠时间:

// Create the timer
System.Timers.Timer aTimer = new System.Timers.Timer(10000);

// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnPrintSimulationResult);

// Change the Interval to change the speed of the simulation
aTimer.Interval = 2000; // <-- Allows you to control the speed of the simulation
aTimer.Enabled = true;
//创建计时器
System.Timers.Timer aTimer=新的System.Timers.Timer(10000);
//连接计时器的已用事件。
aTimer.Appead+=新的ElapsedEventHandler(OnPrintSimulationResult);
//更改间隔以更改模拟的速度

aTimer.Interval=2000;// 是的,有一种“同时”读/写的方法。有几种方法可以做到这一点:

使用另一个线程: 首先,启动一个负责向控制台写入的线程

Thread t = new Thread(()=>{RunSimulation();});
t.IsBackground = true;
t.Start();
模拟方法的大致思路如下:

public void RunSimulation()
{
    while(running)
    {
        // Puts the thread to sleep depending on the run speed
        Thread.Sleep(delayTime);
        Console.WriteLine("Write your output to console!");
    }
}
其次,可以让主线程不断轮询用户输入,以便进行调整

string input = string.Empty;

while(input.Equals("x", StringComparison.CurrentCultureIgnoreCase)
{
    input = Console.ReadKey();
    switch(input)
    {
    case "+":
        // speeds up the simulation by decreasing the delayTime
        IncreaseSpeed();
        break;
    case "-":
        // slows down the simulation by decreasing the delayTime
        DecreaseSpeed();
        break;
    default:
        break;
    }
}
使用计时器: 另一种方法是使用[Timer][1]并调整计时器上的回调频率,而不是调整线程上的睡眠时间:

// Create the timer
System.Timers.Timer aTimer = new System.Timers.Timer(10000);

// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnPrintSimulationResult);

// Change the Interval to change the speed of the simulation
aTimer.Interval = 2000; // <-- Allows you to control the speed of the simulation
aTimer.Enabled = true;
//创建计时器
System.Timers.Timer aTimer=新的System.Timers.Timer(10000);
//连接计时器的已用事件。
aTimer.Appead+=新的ElapsedEventHandler(OnPrintSimulationResult);
//更改间隔以更改模拟的速度
aTimer.Interval=2000;// 您可以在调用
Console.ReadKey()
之前进行检查。这将允许您检查控制台,查看是否有输入等待(即:用户按+或-)而不阻塞。如果您只是在没有可用输入的情况下不尝试读取,那么您的主线程将永远不会阻止等待用户

使用此机制,您实际上可以在单线程应用程序中执行此操作。

您可以在调用
Console.ReadKey()
之前进行检查。这将允许您检查控制台,查看是否有输入等待(即:用户按+或-)而不阻塞。如果您只是在没有可用输入的情况下不尝试读取,那么您的主线程将永远不会阻止等待用户


使用此机制,您实际上可以在单线程应用程序中执行此操作。

您希望模拟什么,更重要的是,您打算使用什么数据结构来建模问题?我想模拟一个游戏。代表人物的字母在“体育场”内随机移动,当他们靠得太近时,就会互相争斗。基本上,我显示体育场、移动字母和所选角色的信息,以及模拟已运行的秒数。您想模拟什么,更重要的是,您打算使用什么数据结构来模拟问题?我想模拟游戏。代表人物的字母在“体育场”内随机移动,当他们靠得太近时,就会互相争斗。基本上,我显示体育场、移动字母和所选角色的显示信息,以及模拟运行了多少秒。输入永远不能等于“退出”,因为一次只能按一个键…@Reed,yah。。。有点像复制和粘贴代码片段。修正了这一点,但更多的是应该从这里删除的一般概念。我被重定向到这个问题,因为我有一个非常类似的问题。我注意到这个答案是从2011年开始的,考虑到.NET在过去10年中的变化,你会采取不同的线程方法吗?@JansthcirlU自2013年以来,我几乎没有写过任何代码。我怀疑这门语言自2011年以来已经相当成熟了。不幸的是,我不知道它是如何发展的事实上,现在我看到了。。。我怀疑
RunSimulation
循环不能正常工作,因为
delayTime
不是原子的。还有一个更好的名字叫
delayTime
simulationSpeed
:)啊,我明白了,谢谢!输入永远不能等于“退出”,因为一次只能按一个键…@Reed,耶。。。有点像复制和粘贴代码片段。修正了这一点,但更多的是应该从这里删除的一般概念。我被重定向到这个问题,因为我有一个非常类似的问题。我注意到这个答案是从2011年开始的,考虑到.NET在过去10年中的变化,你会采取不同的线程方法吗?@JansthcirlU自2013年以来,我几乎没有写过任何代码。我怀疑这门语言自2011年以来已经相当成熟了。不幸的是,我不知道它是如何发展的事实上,现在我看到了。。。我怀疑
RunSimulation
循环不能正常工作,因为
delayTime
不是原子的。还有一个更好的名字叫
delayTime
simulationSpeed
:)啊,我明白了,谢谢!