怎样在C#里的一个线程中来获取或设置控件的属性
如我运行一个线程
然后在运行当中又想获得其中一个TEXTBOX.TEXT的属性
怎摸样写?。。

解决方案 »

  1.   

    不能直接读,会出现异常
    因为控件都属于FORM的主线程,
    而我重新开辟的线城当然不能直接访问主线城中的变量了啊
      

  2.   

    获取简单,如果线程入口函数所属的类与控件所属的类是同一个的话,可以直接获得;至于修改,则不能直接,参看
    http://blog.csdn.net/knight94/archive/2006/03/21/631238.aspx
      

  3.   

    to  Knight94(愚翁) ( ) 信誉:110  2006-07-07 07:43:00  得分: 0  
     to 如果要设置控件值怎摸写啊参看我上面给的文章,就给出例子了。//--------------------------ThreadFun Class------------------------------------//-----------------------------------------------------------------------------//---File:clsThreadFun.cs//---Description:This class demonstrates how to use thread class.//---Author:Knight//---Date:Mar.21, 2006//-----------------------------------------------------------------------------//------------------------{ ThreadFun Class }----------------------------------using System; namespace CSNewTest{    /// <summary>    /// Summary description for clsThreadFun.    /// </summary>    public class clsThreadFun    {        private string strUserName;        public string UserName        {            get{ return strUserName;}            set{ strUserName = value; }        }                 public clsThreadFun( string sUserName)        {            //            // TODO: Add constructor logic here            //            strUserName = sUserName;        }         /// <summary>        /// Thread interface function        /// </summary>        public void ThreadFun()        {            //Detail thread handle         }    }}        接着,是调用代码:    clsThreadFun myThreadFun = new clsThreadFun( "Test" );    //Set parameter through “myThreadFun.UserName”    Thread myThread = new Thread( new ThreadStart( myThreadFun.ThreadFun ) );    myThread.Start(); 你这是什么啊??
    哪有啊,晕!
      

  4.   

    Sorry!给错地址了 -_-||应该是这个
    http://blog.csdn.net/knight94/archive/2006/03/16/626584.aspx
      

  5.   

    大致思路如下:1、首先你需要提供一个函数来修改控件属性,例如:
    public void SetText( string strValue )
    {
        yourTextBox.Text = strValue;
    }2、在你的线程函数中,需要用Invoke来调用,例如:
    // in your thread function
    MethodInvoker mi = new MethodInvoker( SetText );
    this.Invoke( mi, new object[]{ "test" } );
      

  6.   

    winform
            delegate void SetTextCallback(string text);
            Thread m_thread;
            private void button1_Click(object sender, EventArgs e)
            {
                m_thread = new Thread(new ThreadStart(this.CallThread));
                m_thread.Start();
            }
            private void CallThread()
            {
                this.setText("This text was set safely.");
            }
            private void setText(string text)
            {
                if (this.textBox1.InvokeRequired && this.listBox1.InvokeRequired)
                {
                    SetTextCallback d = new SetTextCallback(setText);
                    this.Invoke(d, new object[] { text });
                }
                else
                {
                    this.textBox1.Text = text;
                }
            }
      

  7.   

    winfx(WPF)
            Thread m_thread;
            delegate void SetTextCallback(string text);
            void OnClick(object sender, RoutedEventArgs e)
            {
                m_thread = new Thread(new ThreadStart(CallThread));
                m_thread.SetApartmentState(ApartmentState.STA);
                m_thread.Start();
            }
            private void CallThread()
            {
                for (int i = 1; i <= 5000; i++)
                {
                    SetText(i.ToString());
                }
            }
            private void SetText(string text)
            {
                if (!textBox1.Dispatcher.CheckAccess())
                {
                    this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                       new SetTextCallback(SetText), text);
                }
                else
                {
                    textBox1.Text = text;
                }        }
      

  8.   

    if (this.textBox1.InvokeRequired && this.listBox1.InvokeRequired)
    有错误,应该是
     if (this.textBox1.InvokeRequired)
    原先写的是两个控件,忘了删除了,不好意思
      

  9.   

    參看:
    http://blog.csdn.net/tjvictor/archive/2006/06/21/818312.aspx