现在有个需求,要同步一个操作,我选择用多线程的方式(三,四个线程左右,不固定,动态添加),原本是把时间控制放在线程里面,发现在主程序上 不好控制线程开关
所以把时间控制写在了外面 每次时间到了在开启关闭线程
问题是每个线程的信息我该如何显示在UI上?

解决方案 »

  1.   

    1 c#本身提供线程池的方式处理
    2 控制线程可以在主线程里,也可以使用一个新的线程,控制线程开关都是一样的啊
    3 时间控制线程可以使用Timer当然也可以使用线程间交互控制
    4 线程信息可以通过获取当前线程来解决,要在UI上显示吗,要考虑线程UI交互的特殊处理
      

  2.   

    用Form.Invoke(.............),给你例子:下面的代码示例显示包含一个委托的控件。委托封装一个将项添加到列表框中的方法,该方法在拥有窗体的基础句柄的线程上使用指定的参数执行。用户单击按钮时,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});
             }
          }
       }