C# 从另一个线程在窗体上添加控件

C# 从另一个线程在窗体上添加控件,c#,invoke,multithreading,C#,Invoke,Multithreading,我试图推迟在我的主窗体中添加控件,目的是加快它的开始时间。我遇到了以下例外情况: 跨线程操作无效: 从线程访问的控件“Form1” 而不是创建它的线程 开 我试图在一个较小的例子中简单地解决这个问题,但问题仍然存在。这是我的密码: using System; using System.Drawing; using System.Threading; using System.Windows.Forms; namespace AddConrolFromAnotherThread { pu

我试图推迟在我的主窗体中添加控件,目的是加快它的开始时间。我遇到了以下例外情况:

跨线程操作无效: 从线程访问的控件“Form1” 而不是创建它的线程 开

我试图在一个较小的例子中简单地解决这个问题,但问题仍然存在。这是我的密码:

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

namespace AddConrolFromAnotherThread {
    public partial class Form1 : Form {

        public Form1() {
            InitializeComponent();
        }


        private void AddButton() { 
            if(this.InvokeRequired){
                this.Invoke(new MethodInvoker(this.AddButton));
            }
            Random random = new Random(2);
            Thread.Sleep(20);
            Button button = new Button();
            button.Size = new Size(50,50);
            button.Location = 
                new Point(random.Next(this.Width),random.Next(this.Height));
                this.Controls.Add(button);
        }

        private void buttonStart_Click(object sender, EventArgs e) {
            Thread addControlThread = 
                new Thread(new ThreadStart(this.AddButton));
            addControlThread.Start();
        }
    }
}
我确实使用了Invoke方法,并检查了InvokeRequiered是否为true,但InvokeRequiered保持“true”。我真的不明白。至少我会预期StackOverflow异常,因为这是一个递归调用


因此,如果有人遇到类似的问题,请告诉我我做错了什么?

请改用匿名方法。解释如下

如果我们有这样的形式:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Thread t = new Thread(new ThreadStart(Start));
        t.Start();
    }

    private void UpdateText()
    {
        button1.Text = "New Text";
    }

    void Start()
    {
        UpdateText();
    }
}
这将抛出一个异常

将UpdateText更改为:

private delegate void MyDelegate();

private void UpdateText()
{
    if (button1.InvokeRequired)
    {
       button1.Invoke(new MyDelegate(UpdateText));
    }
    button1.Text = "New Text";
}
或使用匿名方法:

void Start() 
{
    this.Invoke((MyDelegate)delegate
    {
        UpdateText();
    });
}

private void UpdateText()
{
    button1.Text = "New Text";
}

代码中的问题是您正在添加两个按钮

将代码放在else块的if块之后

private void AddButton() { 
        if(this.InvokeRequired){
            this.Invoke(new MethodInvoker(this.AddButton));
        }
        else {
           Random random = new Random(2);
           Thread.Sleep(20);
           Button button = new Button();
           button.Size = new Size(50,50);
           button.Location = new Point(random.Next(this.Width),random.Next(this.Height));
           this.Controls.Add(button);
        }
    }

仅仅添加一个按钮就使用一个线程是非常昂贵的!改用线程池。

您可以像….一样执行此操作

private void AddButton() { 
    if(this.InvokeRequired) {
        Invoke((MethodInvoker)delegate ()
        {
            Random random = new Random(2);
            Thread.Sleep(20);
            Button button = new Button();
            button.Size = new Size(50,50);
            button.Location = new 
            Point(random.Next(this.Width),random.Next(this.Height));
            this.Controls.Add(button);
     });
}

嗨,萨什。谢谢你的快速回答。但我不确定我们是否相互理解。我确实检查了表单是否需要调用。在我的例子中,我没有更新控件,我只是尝试在表单上添加一个新的控件。区别并没有那么大-您必须创建要添加到与表单相同线程中的控件(例如在初始化组件之后),而不仅仅是使用我描述的方法从单独的线程添加它。