Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            #region 委托绑定方法不带参数
            Console.WriteLine("Main threadId is :" + Thread.CurrentThread.ManagedThreadId);
            Message message = new Message();
        
            Thread thread=new Thread(new ThreadStart(message.ShowMessage));
            thread.IsBackground=true;
            thread.Start();
             Console.WriteLine("Do something ..........!");
            Console.WriteLine("Main thread working is complete!");

            #endregion
            Console.WriteLine("################");
            #region 委托绑定方法带参数
            Person person = new Person();
            person.Name = "john";
            person.Age = 32;
            message.getPerson("Herry", 221);
            Thread thread1 = new Thread(new ThreadStart(message.ShowPerson));
            thread1.IsBackground = true;
            thread1.Start();
            #endregion
            Console.WriteLine("################");
            #region 线程池
            //ThreadPool.SetMaxThreads(10000, 10000);//设置线程池的最大值
            //ThreadPool.QueueUserWorkItem(new WaitCallback(AsyncCallback));
            //Console.ReadKey();

            #endregion 
            NewTaskDelegate task = newTask;
            IAsyncResult asyncResult = task.BeginInvoke(2000, null, null);
            while (!asyncResult.IsCompleted)
            {
                Console.Write("*");
                Thread.Sleep(100);
            }
            
            // EndInvoke方法将被阻塞2秒
            int result = task.EndInvoke(asyncResult);
            Console.WriteLine(result);

            // Supply the state information required by the task.
            ThreadWithState tws = new ThreadWithState(
                "This report displays the number {0}.", 2);

            // Create a thread to execute the task, and then
            // start the thread.
            Thread t = new Thread(new ThreadStart(tws.ThreadProc));
            t.Start();
            Console.WriteLine("Main thread does some work, then waits.");
            t.Join();
            Console.WriteLine(
                "Independent task has completed; main thread ends.");

        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            //Console.WriteLine("Main threadId is:" + Thread.CurrentThread.ManagedThreadId);
            //Message message = new Message();
            //Thread thread = new Thread(new ThreadStart(message.ShowMessage));
            //thread.Start();
            //Console.WriteLine("Do something ..........!");
            //Console.WriteLine("Main thread working is complete!");

            Message msg = new Message();
            var del = new SumDel(msg.Sum);
            Console.WriteLine("begin");
            IAsyncResult result = del.BeginInvoke(5, 6, null, null);
            Console.WriteLine("end");
            var str = del.EndInvoke(result);
            Console.WriteLine("结果:"+str);
            Console.Read();
        }