First, preface
May be a small project size, in addition to adding or removing changes in the project only left the business process, did not know how to understand the long-time is more and more blurred ...
Second, the use of the scene
MSDN:Delegate is a reference type that you can use to encapsulate named or anonymous methods. a delegate is similar to a function pointer in C + +, but a delegate is type-safe and reliable.
- Anonymous methods
1 Static voidMain (string[] args)2 {3Testanonymousmethod (Delegate(stringi) {returnConvert.ToInt32 (i);});4 }5 6 Static intTestanonymousmethod (func<string,int>func)7 {8 returnFunc"Ten");9}
- Delegate type
1 //Definitiondelegate type2 Delegate BOOLComparedelegate (intIintj);3 4 Static voidMain (string[] args)5 {6 //declaring Objects7 //comparedelegate cd = new Comparedelegate (Compare);8Comparedelegate CD =Compare;9Console.WriteLine (CD (Ten, -));Ten One testdelegate (Compare); A } - - //the same method as the delegate type signature the Static BOOLCompare (intIintj) - { - returni >J; - } + - //A delegate declares a method parameter, passing the method as an argument + Static voidtestdelegate (comparedelegate CD) A { atConsole.WriteLine (CD ( -,Ten)); -}
- Event modifier delegate (this blog is written for this one node ...) )
The Msdn:event keyword is used to declare an event in the Publisher class. events are special types of multicast delegates that can only be called from the class or struct in which they are declared (the Publisher Class). If the event is subscribed to by another class or struct, its event handler method is called when the event is raised by the Publisher class.
Once today really thought that delegate and event is the same level of keywords, until yesterday to organize the information only dawned, delegate with the event of the half-dime relationship is not.Delegate equals the keyword class, uses the keyword delegate to define the delegate type, then declares the delegate variable using the delegate type, and the event adornment is available when declaring the variable.
1 class Program2 {3 Delegate BOOLComparedelegate (intIintj);4 5 Static voidMain (string[] args)6 {7Publisher p =NewPublisher ();8P.sample =Method;9P.sample + = Method;//register a second methodTen One //Event can only be used when declaring its class or struct using the = Register event , preventing an externally registered event when accidentally overwriting another locally registered events method AP.sampleevent + =Method; - p.raisesampleevent (); - the //An event-modified delegate can only be called in the class or struct in which it is declared, and a delegate without an event adornment can be called externally -P.sample ("External Test Call"); - } - + Static voidMethod (stringmsg) - { + Console.WriteLine (msg); A } at } - - Public classPublisher - { - Public Delegate voidSampleeventhandler (stringmsg); - in //do not use event adornments - PublicSampleeventhandler Sample; to + //using the event modifier - Public EventSampleeventhandler sampleevent; the * Public Virtual voidraisesampleevent () $ {Panax Notoginseng if(Sample! =NULL) - { theSample ("do not use event adornments"); + } A the if(SampleEvent! =NULL) + { -SampleEvent ("using the event modifier"); $ } $ } -}
- Asynchronous delegate
The BeginInvoke method initiates an asynchronous call. The method has the same parameters as the method you want to execute asynchronously, plus two optional parameters. The first parameter is a AsyncCallback delegate that references the method to invoke when the asynchronous call completes . The second parameter is a user-defined object that passes information to the callback method. BeginInvoke will return immediately without waiting for the asynchronous call to complete . BeginInvoke returns a IAsyncResult that can be used to monitor the progress of an asynchronous call.
The EndInvoke method is used to retrieve the result of an asynchronous call. It can be called at any time after the call to BeginInvoke. If the asynchronous call has not yet completed, then EndInvoke blocks the calling thread until the asynchronous call is completed . The parameters of EndInvoke include the out and ref parameters of the method you need to execute asynchronously, and the BeginInvoke returned by IAsyncResult.
1 Static voidMain (string[] args)2 {3Console.WriteLine ("Main thread Start");4action<string> act = msg +5 {6Thread.Sleep ( +);7 Console.WriteLine (msg);8 };9 //Asynchronous InvocationTenIAsyncResult result = Act. BeginInvoke ("Delegate Method",NULL,NULL); One AConsole.WriteLine ("Main thread ... "); - - //Asynchronous Call completion return the Act. EndInvoke (result); - -Console.WriteLine ("Main thread End"); -}
1 Static voidMain (string[] args)2 {3Console.WriteLine ("Main thread Start");4action<string> act = msg +5 {6Thread.Sleep ( +);7 Console.WriteLine (msg);8 };9 Ten //asynchronous calls, without blocking processes OneAct. BeginInvoke ("Delegate Method", AR = A { -action<string> handler = (action<string>) ((AsyncResult) AR). AsyncDelegate; - handler. EndInvoke (AR); the Console.WriteLine (AR. asyncstate); -},"Asyncstate:ok"); -Console.WriteLine ("Main thread End"); -Thread.Sleep ( -); +}
Carding delegate related concepts