简体   繁体   English

Control.Invoke来自运行表单的同一个线程

[英]Control.Invoke from same thread where the form is running

In C#, is there any problem using Control.Invoke to change a property of a Control from the same thread where the form is running? 在C#中,使用Control.Invoke从运行表单的同一线程更改Control的属性是否有任何问题?

I know it would be best using Control.Property = value , but I'd like to know which are the consequences of using Control.Invoke instead. 我知道最好使用Control.Property = value ,但我想知道使用Control.Invoke的后果是什么。

Example: 例:

Using this: 使用这个:

public partial class FormMain : Form
{
    private void Button1_Click(object sender, EventArgs e)
    {
        this.Invoke(new delegate {Label1.Text = "Hello"});
    }
}

Instead of this: 而不是这个:

public partial class FormMain : Form
{
    private void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Hello";
    }
}
this.Invoke(new Action(
    delegate()
    {
        label2.Text = "Test";
    }));

or 要么

this.Invoke(new MethodInvoker(
    delegate()
    {
        label2.Text = "Test";
    }));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM