我正在尝试创建一个 while 或 for 循环,以使用线程按顺序显示索引字符串或 int,以便它自动更改为下一个索引。这是我到目前为止所拥有的。它在 c# 中,但计划对 java 使用相同的信息。
private void button1_Click(object sender, EventArgs e)
{
string st = "hello my name is miroslav glamuzina";
string[] arr = st.Split(' ');
int i = 0;
while (i < arr.Length)
{
Thread.Sleep(50);
label1.Text = arr[i];
i++;
}
}
最佳答案
TPL 加上 await
让这真的变得简单:
private async void button1_Click(object sender, EventArgs e)
{
string st = "hello my name is miroslav glamuzina";
string[] arr = st.Split(' ');
foreach(string word in arr)
{
label1.Text = word;
await Task.Delay(50);
}
}
关于c# - 使用线程遍历数组以每秒显示一个项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22340096/