Plot C#_ Events

Source: Internet
Author: User

Overview

Today's example of a demo event is a simulation implementation of a file download class in which I define a download event that is used to send a message to the user subscribing to the event during a file download, and this message is encapsulated with the Downloadeventargs class. This message class defines a percent field, which is used to save the percentage of the currently downloaded file, please crossing the appreciation process below:

First, define the message class to be sent to the user (subscribing to the event)

1     Internal classDownloadeventargs:eventargs2     {3         Private ReadOnlyInt32 _percent;//File Download Percentage4          PublicDownloadeventargs (Int32 percent)5         {6_percent =percent;7         }8 9          PublicInt32 PercentTen         { One             Get A             { -                 return_percent; -             } the         } -}

Second, the definition of file download class

This class defines a download event, a method that notifies the user (the event subscriber) when an event occurs onfiledownloaded, a file download method FileDownload, as follows:

1 Internal classFileManager2     {3          Public EventEventhandler<downloadeventargs> DownLoad;//Defining Events4 5                 6         protected Virtual voidonfiledownloaded (Downloadeventargs e)7         {8eventhandler<downloadeventargs> temp =DownLoad;9             if(Temp! =NULL)Ten             { OneTemp This, e); A             } -         } -  the          Public voidFileDownload (stringURL) -         { -             intPercent =0; -              while(Percent <= -) +             { -                 //Simulation Download File +++percent; ADownloadeventargs e =NewDownloadeventargs (percent); atOnfiledownloaded (e);//event firing, sending messages to Subscribers (values for download percentages) -  -Thread.Sleep ( +); -             } -         } -}

III. Client Subscription Events

In the client instantiation file download class, and then bind the event, as follows:

1     class Program2     {3         Static voidMain (string[] args)4         {5FileManager Manager =NewFileManager (); 6Manager. DownLoad + = Manager_download;//Subscribe to Events7Manager. FileDownload ("Http://asdfwqerqasdfs.zip");//Download File8         }9 Ten         /// <summary> One         ///methods to be executed after receiving an event notification A         /// </summary> -         /// <param name= "Sender" >Event Trigger Object</param> -         /// <param name= "E" >message sent over by event (percentage)</param> the         Private Static voidManager_download (Objectsender, Downloadeventargs e) -         { -Console.WriteLine (string. Format ("file downloaded: {0}%", E.percent.tostring ())); -         } +}

Iv. Display of results

V. Illustrations

Vi. Personal Understanding

In fact, the event is to bind a series of subscription methods on a delegate, when the method executes, when the event is triggered, the notification is bound to the method on the delegate to execute.

Summarize

Blogging is not easy, especially for me such a new C #, if you feel that the writing is OK, please praise or reward support, I will work harder to write the article.

Plot C#_ Events

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.