将 time/delay 添加到 foreach 中以读取字符串 / Visual Studio 2019 C# Winforms
Add a time/delay into a foreach for reading a string / Visual Studio 2019 C# Win Forms
我想轻松地逐个字母地读取字符串,但是缓慢地,然后将其放入文本框。
private void btn1_Click(object sender, EventArgs e)
{
string str = "hello world", bar = string.Empty;
foreach (char c in str)
{
bar += c;
richTextBox1.Text = bar;
/// delay??
}
}
将方法转为异步并添加具有所需延迟的 await
。
private async void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = string.Empty;
foreach (char c in "hello world")
{
richTextBox1.Text += c;
await Task.Delay(100);
}
}
我想轻松地逐个字母地读取字符串,但是缓慢地,然后将其放入文本框。
private void btn1_Click(object sender, EventArgs e)
{
string str = "hello world", bar = string.Empty;
foreach (char c in str)
{
bar += c;
richTextBox1.Text = bar;
/// delay??
}
}
将方法转为异步并添加具有所需延迟的 await
。
private async void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = string.Empty;
foreach (char c in "hello world")
{
richTextBox1.Text += c;
await Task.Delay(100);
}
}