-1

我在表单上有一个标签,我想每 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;
    }

它不会在列表中旋转

4

2 回答 2

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();
    }

好的,我知道了 ;-)

于 2013-08-02T14:08:21.250 回答
0

好的....你能记录一下线程吗......我很确定你会找到答案......我记得你需要像这样开始一个线程:

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]
}

祝你今天过得愉快 ...

于 2013-08-02T13:53:06.680 回答