窗体上有空间
TextBox1
如何通过线程之间的安全调用PS 不是设置 不检查线程之间的非法调用为FALSE...那样很不安全..
窗体容易死锁!@!!!1
希望大家能给我一个小例子..
感谢..

解决方案 »

  1.   

    MSDN 的原文
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Threading;   public class MyFormControl : Form
       {
          public delegate void AddListItem(String myString);
          public AddListItem myDelegate;
          private Button myButton;
          private Thread myThread;
          private ListBox myListBox;
          public MyFormControl()
          {
             myButton = new Button();
             myListBox = new ListBox();
             myButton.Location = new Point(72, 160);
             myButton.Size = new Size(152, 32);
             myButton.TabIndex = 1;
             myButton.Text = "Add items in list box";
             myButton.Click += new EventHandler(Button_Click);
             myListBox.Location = new Point(48, 32);
             myListBox.Name = "myListBox";
             myListBox.Size = new Size(200, 95);
             myListBox.TabIndex = 2;
             ClientSize = new Size(292, 273);
             Controls.AddRange(new Control[] {myListBox,myButton});
             Text = " 'Control_Invoke' example ";
             myDelegate = new AddListItem(AddListItemMethod);
          }
          static void Main()
          {
             MyFormControl myForm = new MyFormControl();
             myForm.ShowDialog();
          }
          public void AddListItemMethod(String myString)
          {
                myListBox.Items.Add(myString);
          }
          private void Button_Click(object sender, EventArgs e)
          {
             myThread = new Thread(new ThreadStart(ThreadFunction));
             myThread.Start();
          }
          private void ThreadFunction()
          {
             MyThreadClass myThreadClassObject  = new MyThreadClass(this);
             myThreadClassObject.Run();
          }
       }
       public class MyThreadClass
       {
          MyFormControl myFormControl1;
          public MyThreadClass(MyFormControl myForm)
          {
             myFormControl1 = myForm;
          }
          String myString;      public void Run()
          {
             for (int i = 1; i <= 5; i++)
             {
                myString = "Step number " + i.ToString() + " executed";
                Thread.Sleep(400);
                // Execute the specified delegate on the thread that owns
                // 'myFormControl1' control's underlying window handle with
                // the specified list of arguments.
                myFormControl1.Invoke(myFormControl1.myDelegate,
                                       new Object[] {myString});
             }
          }
       }