usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.ComponentModel;usingSystem.Threading;namespaceasyncprogramdemo{ Public classasynccomponentsample:component { Public Eventaction<Object, breakeventargs>Break ; Public Eventaction<Object, debugcompltedeventargs>completed; Public voidStartdebug () {asyncoperation Asyncop= Asyncoperationmanager.createoperation (1); Action<AsyncOperation> exec =NewAction<asyncoperation>(Execute); Exec. BeginInvoke (Asyncop,NULL,NULL); } Private voidExecute (asyncoperation asyncop) {Thread.Sleep ( +); SendOrPostCallback Callback=NewSendOrPostCallback (Callback); Asyncop.post (Callback,7); Thread.Sleep ( +); SendOrPostCallback Completedcallback=NewSendOrPostCallback (Compltedcallback); Asyncop.postoperationcompleted (Completedcallback,"completed"); } Private voidCallback (Objectlinenumber) { if(Break! =NULL) {Break ( This,NewBreakeventargs ((int) (linenumber)) ; } } Private voidCompltedcallback (Objectmsg) { if(Completed! =NULL) {Completed ( This,NewDebugcompltedeventargs (Msg. ToString ())); } } } Public classBreakeventargs:eventargs { Public intlinenumber; PublicBreakeventargs (intlinenumber) { This. LineNumber =linenumber; } } Public classDebugcompltedeventargs:asynccompletedeventargs { Public stringMSG; PublicDebugcompltedeventargs (stringMSG):Base(NULL,false,NULL) { This. MSG =msg; } }}
Asynchronous Programming Design Pattern Demo-asynccomponentsample