当前位置:编程学习 > C#/ASP.NET >>

两个线程建立两个Win窗口,怎么互相控制控件的属性啊?

两个线程建立两个Win窗口
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace 多线程相互控制窗口
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Thread th1 = new Thread(new ThreadStart(Program.run1));
            Thread th2 = new Thread(new ThreadStart(Program.run2));

            th1.Start();
            th2.Start();
        }

        public static void run1() { 
        
        Application.Run(new Form1());
        
        }
        public static void run2()
        {

            Application.Run(new Form2());

        }
    }
}

像这样,两个窗口中都各有一button1, 怎么样才能点Form1中的button1 改变Form2中button1.Text的属性啊?? --------------------编程问答-------------------- 跨线程操作控件?
阿门。  --------------------编程问答-------------------- 这是一个简单的例子,希望对你有帮助!

首先在窗体上,创建一个listbox,lable.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

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

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

        }

        /// <summary>
        /// 定义一个代理
        /// </summary>
        private delegate void CrossThreadOperationControl();

        private void BackgroundProcess()
        {
            // 将代理实例化为一个匿名代理
            CrossThreadOperationControl CrossDelete = delegate()         
            {           
                int i = 1;
                while (i<5)
                {
                   // 向列表框增加一个项目
                    listBox1.Items.Add("Item " + i.ToString());                   
                    i++;
                }
                label1.Text = "我在新线程里访问这个lable!";
                listBox1.Items.Add(label1.Text);
            }  ;
            listBox1.Invoke(CrossDelete);           
        }       

    }
}

--------------------编程问答-------------------- 所有的private 改为public --------------------编程问答-------------------- 我是两个窗口分别为两个线程,他们之间的控件能互相访问么?
楼上的例子 是在线程里的, --------------------编程问答-------------------- 能说清楚吗? button的属性我都改成public 

   private void button1_Click(object sender, EventArgs e)
        {

            Form2 ft = new Form2();
            ft.button1.text = "11";

        }
Form1中的案件事件我写这这样,点击没效果。。 --------------------编程问答-------------------- 没人能回答了么~~
--------------------编程问答-------------------- 顶下 
这情况没试过 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,