有一个服务器/客户端程序,在客户端连接到服务器时启动一个线程,现在我想在启动这个线程时创建一个button,点击这个button后,建立一个dataGridView,在dataGridView中显示数据库的信息。
现在线程里创建一个button搞不定,应该是用委托、时间吧,但是搞不好,另外,连接几十台客户机,button的命名也是一个问题,点击每个button的处理也要单独编写吗?

解决方案 »

  1.   


        public delegate void delTest();    private void button1_Click(object sender, EventArgs e)
        {
          Thread thread = new Thread(new ThreadStart(AddButton));
          thread.IsBackground = true;
          thread.Start();
        }    private void AddButton()
        {
          if (InvokeRequired)
          {
            delTest del = delegate() 
            {
              Button bt = new Button();
              bt.Text = "Test";
              bt.Name = "test";
              bt.Location = new Point(50, 50);
              bt.Click += new EventHandler(bt_Click);
              this.Controls.Add(bt);
            };
            this.Invoke(del);
          }
        }    void bt_Click(object sender, EventArgs e)
        {
          DataGridView dgv = new DataGridView();
          //........
          this.Controls.Add(dgv);
        }
      

  2.   

    new Thread((ThreadStart)delegate
      {
         Button bt = new Button();
              bt.Text = "Test";
              bt.Name = "test";
              bt.Location = new Point(50, 50);
              bt.Click += new EventHandler(bt_Click);
              this.Controls.Add(bt);
      }).Start();
      

  3.   

    是这样,我的客户端连接后的线程写好了,我是想启动这个线程时创建一个button,是不是要在这个线程里发送一个消息,在form1.cs里接收消息,创建button呢?
      

  4.   

    是这样,我的客户端连接后的线程写好了,我是想启动这个线程时创建一个button,是不是要在这个线程里发送一个消息,在form1.cs里接收消息,创建button呢?
      

  5.   

    不好意思,我是初学,您的程序是不是点击button后创建一个button?怎样和我的客户端程序联系起来?
      

  6.   

    既然楼主是刚入门,Invoke 机制有的你学了。
      

  7.   

    我的客户端线程放在一个类里,没在form1.cs里
      

  8.   

    嗯,您能不能提示一个思路,能够解决我的这个问题。谢谢了!
    我今天刚听说Invoke 机制,
      

  9.   

    MSDN 帮助文档里搜索 Invoke,看看美国人是怎样教我们如何用这个的吧。
      

  10.   

    Invoke 机制主要运用于线程内动态添加控件的情况,因为 C# 不允许按常规编程方式在线程内动态添加控件。下面是 MSDN 中的一段示例。
    下面的代码示例显示包含一个委托的控件。委托封装一个将项添加到列表框中的方法,该方法在拥有窗体的基础句柄的线程上使用指定的参数执行。用户单击按钮时,Invoke 运行此委托。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});
             }
          }
       }这只是一种写法,还有别的写法,这里就不说了。
      

  11.   

    就是说,在线程里添加button啥的一定要用Invoke了?是么?
    我开始想把下面的代码加进去,总是不行
    private void InitializeMyButton()
     {
        Button button1 = new Button();    button1.DialogResult = DialogResult.OK;    Controls.Add(button1);
     }