ASP.NET温故而知新学习系列之ASP.NET多线程编程—.NET下的多线程编程Thread中委托的使用(六)

  阅读目录

  一:实例

  一:实例

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading;

  namespace ThreadDelegate
  class Program
     {
        static void Main(string[] args)
        {
            Thread thread = SimpleThread.CreateThread(new SimpleThread.MyDelegate(User.DelegateMethod), "从小就犯困");
            thread.Start();
            thread.Join(Timeout.Infinite);
            Console.ReadKey();
        }
    }

    class User
    {
        //step2定义一个静态方法
        public static void DelegateMethod(object obj)
        {
            Console.WriteLine("我的名字叫:" + obj);
        }
    }
    
    class SimpleThread
    {
        //step1声明一个委托
        public delegate void MyDelegate(object obj);

          /// <summary>
          /// 创建一个用户类
        /// </summary>
         public class User
         {
              public object _name;//名字
              public MyDelegate mydelegate;
              /// <summary>
              /// 得到名字
              /// </summary>
              public void GetName()
              {
                  mydelegate(_name);
              }
         }

         /// <summary>
         /// 创建一个线程
         /// </summary>
         /// <param name="mydelegate"></param>
         /// <param name="name"></param>
         /// <returns></returns>
         public static Thread CreateThread(MyDelegate mydelegate, object name)
         {
              User user = new User();
              user._name = name;
              user.mydelegate = mydelegate;
              Thread thread = new Thread(user.GetName);
              return thread;
         }
     }
 }

 

 

posted @ 2012-03-28 15:21  David.Meng  阅读(840)  评论(0编辑  收藏  举报