Пример #1
0
        //static List<UserInfo> userInfoList = new List<UserInfo>();

        static void Main(string[] args)
        {
            ThreadDemoClass demoClass = new ThreadDemoClass();
            UserInfo        userInfo  = null;

            //创建一个委托并绑定方法
            MyDelegate myDelegate = new MyDelegate(demoClass.Run);

            //创建一个回调函数的委托
            AsyncCallback asyncCallback = new AsyncCallback(Complete);

            //回调函数的参数
            string str = "I'm the parameter of the callback function!";

            for (int i = 0; i < 3; i++)
            {
                userInfo      = new UserInfo();
                userInfo.Name = "Brambling" + i.ToString();
                userInfo.Age  = 33 + i;

                //传入参数并执行异步委托,并设置回调函数
                IAsyncResult result = myDelegate.BeginInvoke(userInfo, asyncCallback, str);
            }

            Console.WriteLine("Main thread working...");
            Console.WriteLine("Main thread ID is:" + Thread.CurrentThread.ManagedThreadId.ToString());
            Console.WriteLine();

            Console.ReadKey();
        }
Пример #2
0
        static void Main(string[] args)
        {
            //Console.WriteLine("Main threadId is:" + Thread.CurrentThread.ManagedThreadId);

            //Thread thread = new Thread(new ThreadStart(AsyncThread));
            //thread.IsBackground = true;
            //thread.Start();
            //thread.Join();

            ThreadDemoClass demoClass = new ThreadDemoClass();

            //设置线程池按需创建的线程的最小数量
            //第一个参数为由线程池根据需要创建的新的最小工作程序线程数
            //第二个参数为异步 I/O 线程数
            ThreadPool.SetMinThreads(5, 5);

            //设置同时处于活动状态的线程池的线程数,所有大于次数目的请求将保持排队状态,直到线程池变为可用
            //第一个参数为线程池中辅助线程的最大数目
            //第二个参数为异步 I/O 线程数
            ThreadPool.SetMaxThreads(100, 100);

            //使用委托绑定线程池要执行的方法(无参数)
            WaitCallback waitCallback1 = new WaitCallback(demoClass.Run1);

            //将方法排入队列,在线程池变为可用时执行
            ThreadPool.QueueUserWorkItem(waitCallback1);

            //使用委托绑定线程池要执行的方法(有参数)
            WaitCallback waitCallback2 = new WaitCallback(demoClass.Run1);

            //将方法排入队列,在线程池变为可用时执行
            ThreadPool.QueueUserWorkItem(waitCallback2, "Brambling");

            UserInfo userInfo = new UserInfo();

            userInfo.Name = "Brambling";
            userInfo.Age  = 33;
            //使用委托绑定线程池要执行的方法(自定义类型的参数)
            WaitCallback waitCallback3 = new WaitCallback(demoClass.Run2);

            //将方法排入队列,在线程池变为可用时执行
            ThreadPool.QueueUserWorkItem(waitCallback3, userInfo);

            Console.WriteLine();
            Console.WriteLine("Main thread working...");
            Console.WriteLine("Main thread ID is:" + Thread.CurrentThread.ManagedThreadId.ToString());
            Console.ReadKey();
        }
Пример #3
0
        static void Main(string[] args)
        {
            ThreadDemoClass demoClass = new ThreadDemoClass();

            ////方式1:创建一个新的线程
            //Thread thread = new Thread(demoClass.Run);

            //方式2:创建一个委托,并把要执行的方法作为参数传递给这个委托
            //ThreadStart threadStart = new ThreadStart(demoClass.Run);
            //Thread thread = new Thread(threadStart);

            //方式3:创建一个委托,并把要执行的方法作为参数传递给这个委托
            ParameterizedThreadStart threadStart = new ParameterizedThreadStart(demoClass.Run);
            Thread thread = new Thread(threadStart);

            UserInfo userInfo = new UserInfo();

            userInfo.Name = "Brambling";
            userInfo.Age  = 333;

            //设置为后台线程
            thread.IsBackground = true;

            ////方式1、2:
            ////开始线程
            //thread.Start();
            //方式3:开始线程,并传入参数
            thread.Start(userInfo);


            ////等待直到线程完成
            //thread.Join();


            Console.WriteLine("Main thread working...");
            Console.WriteLine("Main thread ID is:" + Thread.CurrentThread.ManagedThreadId.ToString());//主线程
            Console.ReadKey();
        }