标签每x秒c#array更改一次文本

时间:2013-08-02 13:44:26

标签: .net timer label

我在表单上有一个标签,我希望每隔3秒使用string []数组中的值更改标签。我想无休止地轮换字符串数组来更新标签。

    public void rotateMarqueText(string text)
    {
        string[] result = text.Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        newsPostCount = result.Count();
        new Task(() =>
        {
            foreach (var a in result)
            {
                DisplayText(a);
                Thread.Sleep(3000);
                return true;
            }

        }
   ).Start();

    }



    private System.Windows.Forms.Timer timer;


    private void DisplayText(string x)
    {
        marqueText.Text = x;
    }

它不会在列表中旋转

2 个答案:

答案 0 :(得分:1)

     public void rotateMarqueText(string text)

    {
        string[] result = "test\nme\n\please\n".Split(new string[] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

        new Task(() =>
        {
            int i = result.Count();
            while (true)
            {
                i++;
                if (i > result.Count()) i = 0;
                Task.Factory.StartNew(() =>
                {
                    this.Invoke(new Action(() => DisplayText(result[i])));
                });
                Thread.Sleep(1000);
            }
        }).Start();
    }
    private void DisplayText(string x)
    {
        marqueText.Text = x;
        marqueText.Refresh();
    }

好的,得到了​​; - )

答案 1 :(得分:0)

oK ....你可以记录一下THREADS ......我很害羞你会找到答案......从wath我记得你需要开始这样一个线程:

Thread myTh = new Thread();
while(....) {  //put a condition...how much to run the thread ex: untill you pres a button
myTh.Sleep(2000);  // sleeps for 2 sec 
label.text = your value from array[i]
}

度过愉快的一天......