c# - 使用线程遍历数组以每秒显示一个项目

标签 c# multithreading winforms while-loop

我正在尝试创建一个 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/

相关文章:

c# - 为什么这不起作用? (通用等于助手)

java - 使用 SynchronousQueue 解决消费者生产者并发问题。公平属性不起作用

c# - 带有列表框的 foreach 的奇怪问题

c# - 在 Winforms 应用程序中实现保存并退出

c# - 虚方法的重载解析

c# - 清除空MVC部分 View 的干净方法

java - 2 个线程中 wait() 和 notifyall() 调用期间的非 volatile 变量值

c# - 如何读取 Winform 上的键盘输入?

c# - Asp.net 身份 我可以编写什么 linq 查询来返回具有其角色的所有用户(包括没有角色的用户)?

c# - 使用 Interlocked.Exchange 更新引用和 Int32