事件驅動非同步模式

來源:互聯網
上載者:User

標籤:c#

事件驅動非同步模式

 

前言

 

啥叫事件?啥叫驅動?非同步又是啥玩意?這些字一個一個的我都認識,但是練起來我就不知道啥意思了,別急,往下看.

 

在下一篇文章中,我會專門介紹並發,同步,非同步以及事件驅動變成的相關技術.

 

Event-based asynchronous(EAP)在多線程的環境中提供了一個簡單的處理方式.

 

它有以下幾個特性

 

1.支援取消

2.可以安全的更新WPF或windows Form空間

3.在completion event中可以查詢異常資訊.

4.“在後台”執行耗時任務(例如下載和資料庫操作),但不會終端您的應用程式

5.同時執行多個操作,每個操作完成時都會接到通知.

6.等待資源變得可用,但不會停止(“掛起”)你得應用程式

7.使用熟悉的事件和委託模型與掛起的非同步作業通訊

 

EAP僅僅是一個模式而已,所以這些特性必須都由實現者來實現.在.net中有少數幾個類支援這種模式,最著名的就是BackgroundWorker和System.Net.WebClient了.

 

這個模式的本質是:每個類都提供了一些相似的成員來管理多線程,例如:

     

   public byte[] DownloadData(Uri address);        public void DownloadDataAsync(Uri address);        public void DownloadDataAsync(Uri address, object userToken);        public event DownloadDataCompletedEventHandler DownloadDataCompleted;         public void CancelAsync(); //取消操作        public bool IsBusy { get; } //擷取是否正在啟動並執行資訊。

 

下面是使用WebClient的例子:

    class Program    {        static void Main(string[] argss)        {            var wc = new WebClient();            wc.DownloadStringCompleted += (sender, args) =>                {                    if (args.Cancelled)                    {                        Console.WriteLine("Canceled");                    }                    else if (args.Error != null)                    {                        Console.WriteLine("Exception:" + args.Error.Message);                    }                    else                    {                        Console.WriteLine(args.Result.Length + " chars were downloaded");                    }                 };            wc.DownloadStringAsync(new Uri("http://www.cnblogs.com/LoveJenny/"));             Console.ReadLine();        }      } 


分析:雖然一個WebClient有多個非同步方法呼叫,但是因為他們都共用了相同的CancelAsyc和IsBusy屬性,所以一次只能有一個非同步作業.

 

 

 

BackgroundWorker

 

BackgroundWorker是System.ComponentModel下面的一個管理背景工作執行緒的協助類,提供了下面幾個特性.

 

1.支援取消

2.可以安全的更新WPF或windows Forms控制項

3.在completion event中可以查詢異常資訊.

4.可以報告進度

5.因為實現了IComponent介面,所以可以被設計器使用.

6.BackgroundWorker使用了線程池,這意味著你永遠都不能在一個BackgroundWorker線程上調用Abort方法.

 

    class Program    {        static BackgroundWorker _bw = new BackgroundWorker();        public static void MainThread()        {            _bw.DoWork += new DoWorkEventHandler(_bw_DoWork);            _bw.RunWorkerAsync("Message to worker");            Console.ReadLine();        }        static void _bw_DoWork(object sender, DoWorkEventArgs e)        {            Console.WriteLine(e.Argument);            //做一些耗時的事情            for (int i = 0; i < 100000; i++)            {                Console.WriteLine(i);            }        }        static void Main(string[] argss)        {            MainThread();            Console.ReadLine();        }      } 


下面的案例實現了進度報告

using System;using System.Collections.Generic;using System.ComponentModel;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks; namespace 事件非同步{    class Program    {        static void Main(string[] args)        {            ThreadBackgroundWorker.MainThread();            Console.ReadLine();        }    }    class ThreadBackgroundWorker    {        static BackgroundWorker _bw;        public static void MainThread()        {            _bw = new BackgroundWorker            {                WorkerReportsProgress=true,//允許報告進度                WorkerSupportsCancellation =true//允許取消            };             _bw.DoWork += new DoWorkEventHandler(_bw_DoWork);            _bw.ProgressChanged += new ProgressChangedEventHandler(_bw_ProgressChanged);            _bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_bw_RunWorkerCompleted);            _bw.RunWorkerAsync("hello to worker");            Console.WriteLine("Press Enter in the next 5 seconds to cancel.");            Console.ReadLine();             if (_bw.IsBusy)            {                _bw.CancelAsync();            }            Console.ReadLine();        }        static void _bw_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)        {            if (e.Cancelled)//是否取消            {                Console.WriteLine("you canceled!");            }            else if (e.Error!=null)            {                Console.WriteLine("worker exception"+e.Error.ToString());            }            else            {                Console.WriteLine("Completed"+e.Result);            }        }         static void _bw_ProgressChanged(object sender, ProgressChangedEventArgs e)        {            //輸出進度報告             Console.WriteLine("Reacher"+e.ProgressPercentage+"%");        }         static void _bw_DoWork(object sender, DoWorkEventArgs e)        {            for (int i = 0; i <=100; i+=20)            {                if (_bw.CancellationPending)                {                    e.Cancel = true;                    return;                }                _bw.ReportProgress(i);//報告進度                 Thread.Sleep(1000);             }            e.Result = 123;        }    }} 


分析:雖然我不知道這是個什麼玩意,想幹嘛,但是看著輸出感覺老酷了,好炫,如果用到其他地方,比如你在家在一個網頁的時候,提示你載入情況,你在下片的時候提示你下了多少,如果用的好感覺很酷,本屌才疏學淺沒啥學問,不知道如何應用,等到以後慢慢來吧.

 

 

 

小小的結一下

 

說實話,這些東西已經超出我的個人理解範圍了,已經不明白是個啥意思了,但是還是硬著頭皮看完了,因為我覺得這就像我小時候老師讓我背過三字經一樣,那個時候我才上小學1年級,字都認不全,更別說理解三字經啥意思了,但是我當時背過了,隨著年齡的增長,我就慢慢理解啥意思了,我相信做學問也差不多,現在因為我不會,不懂,就不學,那肯定將來還是不會!

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

事件驅動非同步模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.