在非UI线程中创建控件 [英] Creating Control in non UI thread

查看:75
本文介绍了在非UI线程中创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好同事

是否可以在非UI线程中创建控件,然后将其与在UI线程中创建的控件一起使用?怎么样?

谢谢,周末愉快!

Hello colleagues

Is it possible to create a control in the non UI thread and then use it with controls created in the UI thread? How?

Thanks and happy weekend!

推荐答案

跨线程通信确实是可能的.

这里看看 [此处 [ ^ ].
Cross thread communication is indeed possible.

Have a look here[^] and here[^].


此代码在UI或后台线程中创建并添加按钮

this code create and add a button either in UI or background thread

<br />
<pre lang="cs">public partial class Form1 : Form<br />
    {<br />
        public Form1()<br />
        {<br />
            InitializeComponent();<br />
        }<br />
        public void CreatingControl()<br />
        {<br />
            if (this.InvokeRequired)<br />
            {<br />
                this.Invoke(new d(this.CreatingControl));<br />
            }<br />
            else {<br />
                Button b = new Button();<br />
                this.Controls.Add(b);<br />
            }<br />
        }<br />
        public delegate void d();<br />
<br />
        private void button1_Click(object sender, EventArgs e)<br />
        {<br />
            Thread t = new Thread(new                    ThreadStart(this.CreatingControl));<br />
            t.Start();<br />
        }<br />
<br />
        private void button2_Click(object sender, EventArgs e)<br />
        {<br />
            this.CreatingControl();<br />
        }<br />
    }</pre><br />
<br />


对不起,我没有清楚自己的意思.

问题是:

如果我在非UI线程中创建控件,是否可以将其添加到在UI线程中创建的控件的子控件中?
I''m sorry I didn''t made myself clear.

The question is:

If I create a control in a non UI thread, can I add it to the child controls of a control created in the UI thread?


这篇关于在非UI线程中创建控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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