开发者

Changing textbox content from inside the ThreadPool C#

I am using a thread pool to do some tests, how can i change a Textbox.t开发者_如何转开发ext in the form?

        ThreadPool.SetMaxThreads(3, 3);

        for(int i2 = 0; i2 < 100; i2++)
        {
            ThreadPool.QueueUserWorkItem(myInt, i2);
        }
        void myInt(object obj23)
    {
        int value = (int)obj23;
        writeTo(value);
       // code to write textbox1.text += value + "\n";
        Thread.Sleep(10000);

    }

error is because a thread that didnt create the object (textbox1) is trying to change it


Create a simple sample for you...

    private void Form1_Load(object sender, EventArgs e)
    {
        for (int i = 0; i < 100; i++)
        {
            System.Threading.ThreadPool.QueueUserWorkItem(k => {

                textBox2.Invoke(new Action(() => {
                    textBox2.AppendText("k:" + k + "\r\n");
                }));

            } , i);
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜