把整个Form定义加个Volatile关键字,可以直接从不同的线程访问,就是安全性不太好.

解决方案 »

  1.   

    delegate string GetTextCallback(); private string GetText() 

    string rtext;
    if (this.textBox1.InvokeRequired) 

    GetTextCallback d = new GetTextCallback(GetText); 
    this.Invoke(d); 

    else 

    rtext=this.textBox1.Text; 

    return rtext;
    } 不知道这样行不行 没有试  你可以试一下
      

  2.   

    加lock会降低多线程带来的性能提高,只能加Volatile关键字了
      

  3.   

    直接读取最好不要用,用类与类之间传递值方式吧。例如:
    class a 
    {
    public a(string s)
    {}
    }class b
    {
      public void f()
    {
       a a1 = new a("s");
    }
    }
    在构造函数里面传递值
      

  4.   

    //vs2008下通过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 WindowsFormsApplication4
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                ReceiveClass myReceiveClass = new ReceiveClass(this,textBox1);            Thread ListenThread = new Thread(new ThreadStart(myReceiveClass.ReceiveMessage));
                ListenThread.Start();        }
        }    class MyEventArgs : EventArgs
        {
            public string InsertText;
        }    class ReceiveClass
        {
            private Form1 mySender;
            private TextBox pTextBox;        public ReceiveClass(Form1 Sender, TextBox TextBox1)
            {
                mySender = Sender;
                pTextBox = TextBox1;
            }        public void ReceiveMessage()
            {
                    //解决跨线程调用windows窗体控件
                    MyEventArgs MyMessage = new MyEventArgs();
                    mySender.Invoke(new EventHandler(readText), new object[] { mySender, MyMessage });            //读取form1 上textBox1.Text 的值
                    string s=MyMessage.InsertText ;
            }        private void readText(object sender, EventArgs e)
            {
                MyEventArgs ec = (MyEventArgs)e;
                ec.InsertText=pTextBox.Text;
            }
        }}
    http://www.mybuffet.cn