现在有一批数据,我想一个线程处理记录的用户身份,另一个线程处理记录的时间,之后一个线程等到两个线程完成后,根据两个线程的结果把这些数据放到相应的表中,最后一个线程,就等到刚才那个线程完成后,再做一些善后工作.我想向各位求一个这样的线程框架,就是说,线程之间怎么样协调的写个样式给我就行了,例如:
static void Main()
{
    Thread thread1 = new Thread(...)
    .......
}
void ...(...)
{
    //此处放...代码
}
如果觉得分不够的,我可以加,谢谢,谢谢!

解决方案 »

  1.   

    namespace Microsoft.Samples.WinForms.Cs.ThreadMarshal {
        using System;
        using System.ComponentModel;
        using System.Drawing;
        using System.Windows.Forms;
        using System.Threading;
        public class ThreadMarshal : System.Windows.Forms.Form {
            private System.ComponentModel.IContainer components;
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.Button button2;
            private System.Windows.Forms.ProgressBar progressBar1;        private Thread timerThread;        public ThreadMarshal() {            // Windows 窗体设计器所必需的
                InitializeComponent();        }        //在背景线程上执行此函数 - 它封送处理调用以
            //将用户界面更新回前景线程
            public void ThreadProc() {            try {
                    MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
                    while (true) {
                        //在窗体上调用 BeginInvoke
                        this.BeginInvoke(mi);
                        Thread.Sleep(500) ;
                    }
                }
                //当该线程被主线程中断时引发 - 退出该循环
                catch (ThreadInterruptedException) {
                    //只需退出....
                }
                catch (Exception) {
                }
            }        //此函数是从背景线程调用的
            private void UpdateProgress() {            //如果需要,重置启动
                if (progressBar1.Value == progressBar1.Maximum) {
                    progressBar1.Value = progressBar1.Minimum ;
                }            progressBar1.PerformStep() ;
            }        //启动背景线程以更新进度栏
            private void button1_Click(object sender, System.EventArgs e) {
                StopThread();
                timerThread = new Thread(new ThreadStart(ThreadProc));
                timerThread.IsBackground = true;
                timerThread.Start();
            }        //停止背景线程以更新进度栏
            private void button2_Click(object sender, System.EventArgs e) {
                StopThread();
            }        //停止背景线程
            private void StopThread()
            {
                if (timerThread != null)
                {
                    timerThread.Interrupt();
                    timerThread = null;
                }
            }        protected override void Dispose(bool disposing)
            {
               StopThread();
               if (disposing) {
                    if (components != null) {
                        components.Dispose();
                    }
               }           base.Dispose(disposing);
            }        private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.button2 = new System.Windows.Forms.Button();
                this.progressBar1 = new System.Windows.Forms.ProgressBar();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
                this.button1.Location = new System.Drawing.Point(164, 74);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(153, 46);
                this.button1.TabIndex = 1;
                this.button1.Text = "开始!";
                this.button1.Click += new System.EventHandler(this.button1_Click);
                // 
                // button2
                // 
                this.button2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
                this.button2.Location = new System.Drawing.Point(328, 74);
                this.button2.Name = "button2";
                this.button2.Size = new System.Drawing.Size(153, 46);
                this.button2.TabIndex = 1;
                this.button2.Text = "停止!";
                this.button2.Click += new System.EventHandler(this.button2_Click);
                // 
                // progressBar1
                // 
                this.progressBar1.Font = new System.Drawing.Font("宋体", 8F, System.Drawing.FontStyle.Bold);
                this.progressBar1.Location = new System.Drawing.Point(13, 12);
                this.progressBar1.Name = "progressBar1";
                this.progressBar1.Size = new System.Drawing.Size(448, 46);
                this.progressBar1.TabIndex = 2;
                this.progressBar1.Text = "开始!";
                // 
                // ThreadMarshal
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(501, 135);
                this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                              this.button1,
                                                                              this.button2,
                                                                              this.progressBar1});
                this.Name = "ThreadMarshal";
                this.Text = "已使用设计器生成";
                this.ResumeLayout(false);        }        [STAThread]
            public static void Main(string[] args) {
                Application.Run(new ThreadMarshal());
            }
        }
    }
      

  2.   

    private thread1,thread2;static void Main()
    {
    thread1 = new Thread(...)
    thread2 = new Thread(...)
    thread1.Start();
    thread2.Start();
    Thread thread3 = new Thread( new ThreadStart(thProc));
    thread3.Start();
    .......
    }
    void ...(...)
    {
    //此处放...代码
    }void thProc()
    {
    thread1.Join();
    thread2.Join();
    //两个线程搞定//善后}
      

  3.   

    weisunding(鼎鼎)的可以吗?
    thread3不是由thread1和thread创建的,他能知道thread1.Join()、thread2.Join()吗?
    这个问题好象比较麻烦,因为thread3很难访问到thread1和thread2的数据,必须要用到回掉函数或者内存映射
      

  4.   

    to:ybzsu() ()当然可以,在线程3中调用其它线程的.join()怎么不行??
    只是同步等它执行完毕而已,与谁创建的何干!你和楼主都跑一下如下代码

    Thread t1,t2; private void Form1_Load(object sender, System.EventArgs e)
    {
    t1 = new Thread(new ThreadStart(thWork));
    t2 = new Thread(new ThreadStart(thWork));
    Thread t3 = new Thread(new ThreadStart(thProc));
    t1.Start();
    t2.Start();
    t3.Start();
    } void thWork()
    {
    for(int i=0;i<100;i++)
    {
    Console.WriteLine(i.ToString());
    Thread.Sleep(50);
    }
    } void thProc()
    {
    t1.Join();
    t2.Join(); Console.WriteLine("两个都完蛋了!应该我了!!");
    }
      

  5.   

    System.ServiceProcess.ServiceBase[] ServicesToRun;
    ServicesToRun =new System.ServiceProcess.ServiceBase[]{new WatchFiles(inipz)};
    其中 WatchFiles为服务名称