c#控制台应用程序,断开readline(),线程循环?
因此,我在控制台应用程序中有一个方法,用于呈现菜单并等待用户输入:c#控制台应用程序,断开readline(),线程循环?,c#,multithreading,console-application,C#,Multithreading,Console Application,因此,我在控制台应用程序中有一个方法,用于呈现菜单并等待用户输入: public void RenderMainMenu() { Console.WriteLine("Pick one option:"); Console.WriteLine("[1] Option1"); Console.WriteLine("[2] Option2"); var userInput = Console.ReadLine();
public void RenderMainMenu()
{
Console.WriteLine("Pick one option:");
Console.WriteLine("[1] Option1");
Console.WriteLine("[2] Option2");
var userInput = Console.ReadLine();
// ... other code of what do to with user input
}
我还有一个在应用程序开始时创建的类,该类负责在应用程序运行时始终在后台循环列表:
class CheckList
{
Thread t;
List<DateTime> list;
public CheckList()
{
list = new List<DateTime> { DateTime.Now.AddMinutes(5), DateTime.Now.AddMinutes(1) };
t = new Thread(new ThreadStart(LoopList));
t.Start();
}
private void LoopList()
{
foreach (DateTime d in list)
{
if (d >= DateTime.Now)
{
// Get out of main menu and display a new menu now.
}
}
}
}
课程清单
{
螺纹t;
名单;
公众检查表()
{
list=新列表{DateTime.Now.AddMinutes(5),DateTime.Now.AddMinutes(1)};
t=新线程(新线程开始(循环列表));
t、 Start();
}
私有void LoopList()
{
foreach(列表中的日期时间)
{
如果(d>=DateTime.Now)
{
//现在退出主菜单并显示新菜单。
}
}
}
}
我如何做到这一点
甚至有可能在Console.App上实现这样的结果吗?我的意思是:
有一种菜单类型(menu1、menu2、menu3)随时可用,并在导航和做事情时为其获取用户输入,但一旦时间到了,一些菜单会弹出,这个用户输入会转到弹出的新菜单…?我想您正在调用主方法内的另一个线程。除非您共享一些代码,然后我们才能理解您想说什么,否则就不清楚了。你能分享你的源代码吗?这样我们就可以帮你了
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please enter any value");
string str = Console.ReadLine();
ThreadStart childref = new ThreadStart(CallToChildThread);
Thread childThread = new Thread(childref);
childThread.Start();
Console.ReadLine();
}
public static void CallToChildThread()
{
Console.WriteLine("Child thread starts");
// the thread is paused for 5000 milliseconds
int sleepfor = 5000;
Console.WriteLine("Child Thread Paused for {0} seconds", sleepfor / 1000);
Thread.Sleep(sleepfor);
Console.WriteLine("Child thread resumes");
}
}
试着用相关术语清楚地说明你的问题,以帮助他人解决你的问题。目前,还不清楚你想做什么。即使很清楚你想要的“最佳方法”是意见。。不是事实。我没有发现任何东西,只是“发送回车到控制台以断开readline()”-这是唯一的方法吗?对我来说,这似乎不是最好的方法,也许是——谁知道我只是想看看我的选择是什么。