|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
我认为,可以通过更加简单的首次编译,而增加第二次编译的负担,来提高java的运行效率。只是将java源代码进行简单的等价转换,而不假设编译成某种虚拟机器的目标格式,而由本地编译器针对性的二次编译。ThelatestofferingfromMicrosofttosupportsoftwaredevelopmentisthe.NETFramework.InsidethisvastFrameworkistheASP.NET.TheASP.NETfacilitatestheapplicationdevelopmentcommunityingeneratinghighperformanceWebbasedapplicationsthatcanbedatarichandworkinaheterogeneousenvironment.
Inthisdiscussionwewillexaminethestepstocreateacustomeventthatwillalerttheuserorprocessthateventhastakenplaceunderaspecifiedcondition.Tounderstandhowtosetupthecodestructuretoraiseandhandleevents,anoverviewoftheeventarchitectureisneeded.
EventArchitecture
Belowareanumberofkeypointsthatencompassthe.NetFrameworkeventmodel.
-Eventstakingplaceinthe.NETFrameworkarebaseduponthedelegatemodel.
-Thedelegateclassisthemechanismthatallowstheeventsendertocommunicatewiththeeventhandler.
-Aneventsendercanbroadcastaneventtoanumberofeventhandlers.
-Aneventhandlermustberegisteredwithaneventsender.
Thefundamentaldefinitionofaneventis:Aneventisamessagethathasbeensentbyanobjectduetosomeoccurrenceofanactionwithintheworkflowoftheapplication.Thisactioncouldbeabuttonthathasbeenclickedorspecifiedtimeoutparametervaluehasbeenreached.Thisactiontriggerstheeventandthesubsequentmethodthathandlestheeventisinitiatedandtheeventishandled.
Inthe.NETframework,thesendingobjectdoesnotknowwhatmethodwillhandletheevent.Therefore,anagentmustbeutilizedtohandlethecommunication.Thisagentisthedelegateclassthatholdsareferencetothemethodthathandlestheevent.Thedelegateclasshasanintrinsicsignatureandcanonlyholdreferencestomethodsthatobtainthesamesignature.Youcanequivocatethedelegateclasstoatype-safepointertoacallback.Theeventdelegatehastwoparameters;thesearethesendingobjectandthereturneddata.BelowinListing1isgenericdeclarationoftheeventdelegateclass.
Listing1
PublicdelegatevoidEventHandler(objectSender,EventArgse);
Inordertoinitiateandhandlecustomevents,eventfunctionalityneedstobeprovided.Thefundamentalstructureislistedasfollows:
-Aclasstoholdtheeventdata.ThismustbeinheritedfromSystem.EventArgs.
-Adelegatefortheevent.
-Aclassthatraisestheevent.
-Aclasswithamethodthathandlestheevent.
Nowthatwehavediscussedthefundamentalsofeventhandling,weneedasimpleapplicationtodepicttheeventfunctionality.
CustomEventsApplication
TheCustomEventsApplicationiscomprisedofasimpleWebFormthatdisplaystheeventdatamessage.ThisisshowninFigure1.
Figure1
Inordertoinitiatesomeactionthatshowsthefunctionalityofeventhandling,aloopiscreatedtocounttosomenumberandstop.Duringthiscount,aspecificvalueisinterrogatedwithintheloop.Whenthespecifiedvalueisequaltotheloopvalue,theeventisfired.Inthiscasethevalueis400000.Thelooprunsfrom1to500000.Whenthevalue400000isreachedtheOnMessagemethodiscalledandtheeventisraised.Note:TheInitEvent()methodiscalledfromPage_Load()methodwhenthepageisrequested.TheInitEvent()methodisdepictedinListing2.
Listing2
privatevoidInitEvent()
{
MessagingHandlermh=newMessagingHandler();
MessageEventme=newMessageEvent();
me.Message+=newMessageHandler(mh.GetMessageData);
for(inti=1;i<=500000;i++)
{
if(i==400000)
{
MessageEventArgse=newMessageEventArgs();
me.OnMessage(e);
lblMessage.Text=mh.EventMessage;
}
}
}
AsyoucanseethisiscertainlyoverkilltodisplayalineoftextinalabelfieldonaWebpage.However,itisusefultoexaminetheworkflowofaneventthatiscapturedandhandledinthe.NETFramework.InordertomakesenseofthecodefragmentinListing2,weneedtoexaminetheclassesthatcomprisetheeventstructure.BelowinListing3showstherespectivecomponentsthatwillraiseandhandletheeventasittranspires.
Listing3
publicclassMessageEventArgs:EventArgs
{
publicstringMessageText
{
get
{
return("Therehasbeen400000valuesprocessed.");
}
}
}
publicdelegatevoidMessageHandler(objectsender,MessageEventArgse);
publicclassMessageEvent
{
publiceventMessageHandlerMessage;
publicvoidOnMessage(MessageEventArgse)
{
if(Message!=null)
{
Message(this,e);
}
}
}
publicclassMessagingHandler
{
privatestringstrMessage;
publicstringEventMessage
{
get
{
returnstrMessage;
}
set
{
strMessage=value;
}
}
publicvoidGetMessageData(objectsender,MessageEventArgse)
{
stringstrMessage=e.MessageText;
EventMessage=strMessage;
}
}
TheclassMessageEventArgswillsupplythedataforevent.NoticethatitisinheritedfromEventArgs.AsyoucanseeMessageTextisapropertyoftheclassandagetstatementisutilizedtoreturnthedatastring.ThedelegateinthiscaseistheMessageHandler.IthasthesenderobjectandtheMessageEventArgsforthedatainitsargumentlist.MovingontotheclassMessageEvent.ThisclasswillraisetheeventwiththeOnMessage()methodbyinvokingthedelegates.TheMessagingHandlerclasswillextracttheeventmessagefromtheMessageEventArgsandpopulatetheEventMessagepropertysothemessagecanbedisplayedontheWebpage.
Insummarization,wehaveaclassthatsuppliesthedata,adelegatetoprovidethecommunication,aclasstoraisetheevent,andaclasstohandletheevent.Thebasicworkflowisasfollows:
-TheInitEvent()isinitiatedfromthePage_Load()methodwhenthepageisrequested.
-MessagingHandlermh=newMessagingHandler()instantiatestheMessagingHandlerclass-EventReceiver.
-MessageEventme=newMessageEvent()instantiatestheMessageEventclass-EventSender.
-me.Message+=newMessageHandler(mh.GetMessageData)registerstheeventhandlerwiththeeventsourcesothattheMessagingHandlerinstancecanreceivealarmevents.The+=assignmentoperatoraddsthedelegatetothelistofdelegatesthatareregisteredwiththeMessageevent.
-Whenthespecifiedvalueisreached,MessageEventArgse=newMessageEventArgs()isinstantiatedandtheOnMessage()iscalledtoraisetheevent.
-TheOnMessage()methodinvokesthedelegatewhichcallstheGetMessageData()methodtohandletheevent.
-TheGetMessageData()methodobtainsthemessagetextfromtheMessageEventArgsandpopulatestheEventMessagepropertywiththestringdata.
-ThelabelfieldispopulatedwiththeEventMessagestringpropertyvalueanddisplayedontheWebpage.
Runthisexample
[runit]
Conclusion
SinceWebbasedapplicationsaredisconnectedinnatureandthehandlingofeventsaretypicallyserver-sidetheeventmessageisdisplayedaftertheentirecodesequencehastranspired.Thisparadigmisnotconduciveforasynchronousprocessingduetothefire-and-forgetprocessing.However,theycanbeusefulifyouaredoingheavyserver-sideprocessingandvalidationbeforerenderingthepage.
The.NETFrameworkisprovidingtheapplicationdeveloperagreatdealofoptionstogeneratemeaningfulsolutionstotheenterprise,beitWebbasedapplications,WindowsClientbasedapplications,orServerbasedSer
[img=1border=0style=,1src=]http://www.ckuyun.com/[/img]
感觉很多控件都必须自己去写代码;用了WebMatrix感觉也不是很好,毕竟没有很强的WYSIWYG效果。现在就不知道如何是好了。 |
|