異步編程(二)

3.TAP 基於任務的異步編程編程

1..NET4.0 引入了Task任務,Task的使用多線程

Task task = new Task(()=> {
                for (int i = 0; i < 1000; i++)
                {
                    Console.WriteLine("task run");
                }               
            });
            task.Start();

這是簡單的啓動任務:下面是task的一些版本併發

public Task(Action action);
      
        public Task(Action action, CancellationToken cancellationToken);
      
        public Task(Action action, TaskCreationOptions creationOptions);
        
        public Task(Action<object> action, object state);
        
        public Task(Action action, CancellationToken cancellationToken, TaskCreationOptions creationOptions);
      
        public Task(Action<object> action, object state, CancellationToken cancellationToken);
       
        public Task(Action<object> action, object state, TaskCreationOptions creationOptions);
       
        public Task(Action<object> action, object state, CancellationToken cancellationToken, TaskCreationOptions creationOptions);
 

2.創建一個帶取消功能的Task異步

CancellationTokenSource tokenSource = new CancellationTokenSource();
Task task = new Task(() => {
                for (int i = 0; i < 1000000; i++)
                {
                    Console.WriteLine("task run");
                }

            }, tokenSource.Token);
           task.Start();

這裏使用Task的task 將會觀察CancellationToken,當調用異步編程

tokenSource.Cancel() 便可終止任務spa

 

3.使用TaskFactory來建立任務線程

Task.Factory.StartNew(() => {
                Console.WriteLine("task is running");
            });

 

4.任務的管理code

1.WaitAnyblog

var task1= Task.Factory.StartNew(() => {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("task1 is running");
                }                
            });

            var task2= Task.Factory.StartNew(() => {
                Console.WriteLine("task2 is running");
            });

            var taskid= Task.WaitAny(task1, task2);
            Console.WriteLine(taskid);

Task.WaitAny(task1, task2) 返回的是完成任務的id,表示只要有一個任務完成就返回完成任務的Id 不然一直阻塞;token

 

2.WaitAll

var task1= Task.Factory.StartNew(() => {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("task1 is running");
                }                
            });

            var task2= Task.Factory.StartNew(() => {
                Console.WriteLine("task2 is running");
            });

            Task.WaitAll(task1, task2);
            Console.WriteLine("All Task Completed");

image

WaitAll 將等待全部任務的完成,不然將一直阻塞;

 

3.ContinueWith

這個最有意思,在傳統的同步編程模式下,咱們喜歡程序串行執行,而後獲得結果,可是在多線程編程中,任務都是併發的,很難再使用同步的方法來編程多線程,可是Task提供了這個ContinueWith,當異步任務完成的時候,接着執行這個ContinueWith,任務執行有返回結果,則使用相應版本的ContinueWith來執行,而後總體的執行就想同步編程那樣,很方便;

var task1= Task.Factory.StartNew(() => {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("task1 is running");
                }                
            });
            task1.ContinueWith((ta)=> {
                Console.WriteLine("task1 continuewith");
            });

image

 

帶返回值版本:

 

var task1= Task.Factory.StartNew(() => {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("task1 is running");
                }

                return "Hello,world";
            });
            task1.ContinueWith((ta)=> {
                Console.WriteLine("task1 continuewith reulst"+ta.Result);
            });

image

 

4.任務的阻塞

var task1= Task.Factory.StartNew(() => {
                for (int i = 0; i < 20; i++)
                {
                    Console.WriteLine("task1 is running");
                }

                return "Hello,world";
            });
            task1.Wait();
            Console.WriteLine("task1 completed");

image

waite 將阻塞線程,知道task的完成

相關文章
相關標籤/搜索