如何更新从另一个线程在主线程文本框? [英] How to update textboxes in main thread from another thread?

查看:148
本文介绍了如何更新从另一个线程在主线程文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的C#所以我裸露。你如何从运行不同的类一个新的线程更新主线程文本框和标签。

New to C# so bare with me. How you update textboxes and labels in the main thread from a new thread running a different class.

MainForm.cs(主线程)

MainForm.cs (Main thread)

public partial class MainForm : Form
{
    public MainForm()
    {
        Test t = new Test();

        Thread testThread = new Thread(new ThreadStart(t.HelloWorld));
        testThread.IsBackground = true;
        testThread.Start();
    }

    private void UpdateTextBox(string text)
    {
        textBox1.AppendText(text + "\r\n");
    }

}

public class Test
{
    public void HelloWorld()
    {
        MainForm.UpdateTextBox("Hello World"); 
        // How do I execute this on the main thread ???
    }
}



我已经看过了的例子在这里,但不能似乎得到它的权利。请能有人给了一些很好的联系。

I have looked at the examples on here but cant seem to get it right. Please could someone give some good links.

我又开始新的,所以我不要弄乱我的代码。如果有人想放了一个工作示例我的例子,将是巨大的。如果我不得不更新像文本框和标签等多个对象(不是所有在同一时间),这将是去它的最好方式,有一个方法

I have started again fresh so I don't mess up my code. If anyone would like to put up a working example with my example that would be great.

另外每个文本框或有没有办法用一个方法来做到这一点?就像我说我是新的,所以慢慢地说:)

Also if I had to update multiple objects like textboxes and labels etc (not all at the same time) what would be the best way to go about it, having a method for each textbox or is there a way to do this with one method? Like I said I am new so speak slowly :)

推荐答案

调用或BeginInvoke,如:

Invoke or BeginInvoke, e.g.

Invoke((MethodInvoker)delegate {
    MainForm.UpdateTextBox("Hello World"); 
});






@tiptopjones我猜你还怎么问去的形式的参考。你可以让你的HelloWorld方法需要一个对象参数,使用ParameterizedThreadStart委托,然后传递到窗体的引用作为参数传递给Thread.Start方法。但我建议阅读有关匿名方法,这使得它容易得多,并保持一切强类型


@tiptopjones I guess you're asking also how to get a reference to the form. You could make your HelloWorld method take an object parameter, use the ParameterizedThreadStart delegate, and then pass a reference to the form as a parameter to the Thread.Start method. But I would suggest reading about anonymous methods which makes it a lot easier and keeps everything strongly typed.

public class MainForm : Form {
    public MainForm() {
        Test t = new Test();

        Thread testThread = new Thread((ThreadStart)delegate { t.HelloWorld(this); });
        testThread.IsBackground = true;
        testThread.Start();
    }

    public void UpdateTextBox(string text) {
        Invoke((MethodInvoker)delegate {
            textBox1.AppendText(text + "\r\n");
        });
    }
}

public class Test {
    public void HelloWorld(MainForm form) {
        form.UpdateTextBox("Hello World"); 
    }
}

当你舒服,你可以阅读了关于lambda表达式和不喜欢它:

When you get comfortable with that you could read up on lambda expressions and do it like:

Thread testThread = new Thread(() => t.HelloWorld(this));

这篇关于如何更新从另一个线程在主线程文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆