C# 使用计时器延迟char[]一次显示所有字符
我很难找到在控制台上使用计时器一次显示一个字符的最佳方式。这个想法是让一个RPG风格的消息流在屏幕上显示,而不是一次粘贴C# 使用计时器延迟char[]一次显示所有字符,c#,C#,我很难找到在控制台上使用计时器一次显示一个字符的最佳方式。这个想法是让一个RPG风格的消息流在屏幕上显示,而不是一次粘贴 private static System.Timers.Timer aTimer; public void DisplayTime() { aTimer = new System.Timers.Timer(5000); aTimer.Elapsed += new ElapsedEventHandler(OnTime
private static System.Timers.Timer aTimer;
public void DisplayTime()
{
aTimer = new System.Timers.Timer(5000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset = false;
aTimer.Enabled = true;
Console.ReadLine();
}
public void OnTimedEvent(object source, ElapsedEventArgs e)
{
string text;
text = "This is a test";
char[] message = new char[text.Length];
message = text.ToCharArray();
for (int i = 0; i < message.Length; i++)
{
Console.Write(message[i]);
}
}
private static System.Timers.Timer aTimer;
public void DisplayTime()
{
aTimer=新系统计时器计时器(5000);
aTimer.Appead+=新的ElapsedEventHandler(OnTimedEvent);
aTimer.AutoReset=false;
aTimer.Enabled=true;
Console.ReadLine();
}
public void OnTimedEvent(对象源,ElapsedEventArgs e)
{
字符串文本;
text=“这是一个测试”;
char[]message=新字符[text.Length];
message=text.ToCharArray();
for(int i=0;i
这就是我目前的情况,只是延迟显示消息5秒。我尽量避免使用Thread.Sleep,如果可能的话,因为从我读到的内容来看,这是一个很大的禁忌。我知道我需要在for循环中创建一个延迟来获得我需要的效果,但这真的是最好的方法吗?使用
Thread.Sleep()
。它将提供你所需要的,没有任何缺点。您所听到的关于Sleep()
的内容完全是断章取义的,不适用于您的情况。使用Thread.Sleep()
。它将提供你所需要的,没有任何缺点。您所听到的关于Sleep()
的内容完全脱离了上下文,不适用于您的情况。您需要跟踪您在角色流中的位置-似乎您希望一次只显示一个角色,然后暂停一点,直到显示完所有角色。计时器非常适合这样做-您必须将消息和消息中的当前位置作为实例变量进行跟踪:
private int messagePosition = 0;
private string message = "This is a test";;
public void OnTimedEvent(object source, ElapsedEventArgs e)
{
if(messagePosition < message.Length)
{
Console.Write(message[messagePosition]);
messagePosition++;
}
else
aTimer.Enabled = false;
}
private int messagePosition=0;
私有字符串消息=“这是一个测试”;;
public void OnTimedEvent(对象源,ElapsedEventArgs e)
{
if(messagePosition
您需要跟踪您在字符流中的位置-似乎您希望一次只显示一个字符,然后暂停一点,直到显示完所有字符。计时器非常适合这样做-您必须将消息和消息中的当前位置作为实例变量进行跟踪:
private int messagePosition = 0;
private string message = "This is a test";;
public void OnTimedEvent(object source, ElapsedEventArgs e)
{
if(messagePosition < message.Length)
{
Console.Write(message[messagePosition]);
messagePosition++;
}
else
aTimer.Enabled = false;
}
private int messagePosition=0;
私有字符串消息=“这是一个测试”;;
public void OnTimedEvent(对象源,ElapsedEventArgs e)
{
if(messagePosition
这是使用lambda的好地方
public void DisplayTime(string message) {
var index = 0;
var timer = new System.Timers.Timer(5000);
timer.Elapsed += delegate {
if (index < message.Length) {
Console.Write(message[index]);
index++;
} else {
timer.Enabled = false;
timer.Dispose();
}
};
timer.Enabled = true;
}
public void显示时间(字符串消息){
var指数=0;
var定时器=新系统定时器定时器定时器(5000);
timer.appeased+=委托{
if(索引<消息长度){
Console.Write(消息[索引]);
索引++;
}否则{
timer.Enabled=false;
timer.Dispose();
}
};
timer.Enabled=true;
}
现在,您只需调用
DisplayTime(“消息”)
,它将每5秒缓慢地写出一个字符的消息,并在完成时清理计时器。这是使用lambda的好地方
public void DisplayTime(string message) {
var index = 0;
var timer = new System.Timers.Timer(5000);
timer.Elapsed += delegate {
if (index < message.Length) {
Console.Write(message[index]);
index++;
} else {
timer.Enabled = false;
timer.Dispose();
}
};
timer.Enabled = true;
}
public void显示时间(字符串消息){
var指数=0;
var定时器=新系统定时器定时器定时器(5000);
timer.appeased+=委托{
if(索引<消息长度){
Console.Write(消息[索引]);
索引++;
}否则{
timer.Enabled=false;
timer.Dispose();
}
};
timer.Enabled=true;
}
现在,您只需调用DisplayTime(“消息”)
,它将每隔5秒缓慢地写出一个字符的消息,并在完成时清理计时器,使用Thread.Sleep()在控制台应用程序中实际上并不重要。这在GUI应用程序中是唯一的邪恶,这种应用程序对用户输入保持响应。在控制台应用程序中使用Thread.Sleep()实际上并不重要。这是GUI应用程序中唯一的邪恶之处,这种应用程序对用户输入保持响应。