Delphi Thread Pool Example Using AsyncCalls 异步调用Word文档下载推荐.docx
- 文档编号:15279653
- 上传时间:2022-10-29
- 格式:DOCX
- 页数:6
- 大小:18.16KB
Delphi Thread Pool Example Using AsyncCalls 异步调用Word文档下载推荐.docx
《Delphi Thread Pool Example Using AsyncCalls 异步调用Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《Delphi Thread Pool Example Using AsyncCalls 异步调用Word文档下载推荐.docx(6页珍藏版)》请在冰豆网上搜索。
Torepeatmygoal:
transformmysequential"
of500-2000+filesfromthenonthreadedapproachtoathreadedone.Ishouldnothave500threadsrunningatonetime,thuswouldliketouseathreadpool.Athreadpoolisaqueue-likeclassfeedinganumberofrunningthreadswiththenexttaskfromthequeue.
Thefirst(verybasic)attemptwasmadebysimplyextendingtheTThreadclassandimplementingtheExecutemethod(mythreadedstringparser).
SinceDelphidoesnothaveathreadpoolclassimplementedoutofthebox,inmysecondattemptI'
vetriedusingOmniThreadLibrarybyPrimozGabrijelcic.
OTLisfantastic,haszillionwaystorunataskinabackground,awaytogoifyouwanttohave"
fire-and-forget"
approachtohandingthreadedexecutionofpiecesofyourcode.
AsyncCallsbyAndreasHausladen
Note:
whatfollowswouldbemoreeasytofollowifyoufirstdownloadthesourcecode.
WhileexploringmorewaystohavesomeofmyfunctionsexecutedinathreadedmannerI'
vedecidedtoalsotrythe"
AsyncCalls.pas"
unitdevelopedbyAndreasHausladen.Andy'
sAsyncCalls–AsynchronousfunctioncallsunitisanotherlibraryaDelphidevelopercanusetoeasethepainofimplementingthreadedapproachtoexecutingsomecode.
FromAndy'
sblog:
WithAsyncCallsyoucanexecutemultiplefunctionsatthesametimeandsynchronizethemateverypointinthefunctionormethodthatstartedthem....TheAsyncCallsunitoffersavarietyoffunctionprototypestocallasynchronousfunctions....Itimplementsathreadpool!
Theinstallationissupereasy:
justuseasynccallsfromanyofyourunitsandyouhaveinstantaccesstothingslike"
executeinaseparatethread,synchronizemainUI,waituntilfinished"
.
Besidethefreetouse(MPLlicense)AsyncCalls,AndyalsofrequentlypublisheshisownfixesfortheDelphiIDElike"
DelphiSpeedUp"
and"
DDevExtensions"
I'
msureyou'
veheardof(ifnotusingalready).
AsyncCallsInAction
Whilethere'
sonlyoneunittoincludeinyourapplication,theasynccalls.pasprovidesmorewaysonecanexecuteafunctioninadifferentthreadanddothreadsynchronization.TakealookatthesourcecodeandtheincludedHTMLhelpfiletogetfamiliarwiththebasicsofasynccalls.
Inessence,allAsyncCallfunctionsreturnanIAsyncCallinterfacethatallowstosynchronizethefunctions.IAsnycCallexposesthefollowingmethods:
//v2.98ofasynccalls.pas
IAsyncCall=interface
//waitsuntilthefunctionisfinishedandreturnsthereturnvalue
functionSync:
Integer;
//returnsTruewhentheasynchronfunctionisfinished
functionFinished:
Boolean;
//returnstheasynchronfunction'
sreturnvalue,whenFinishedisTRUE
functionReturnValue:
//tellsAsyncCallsthattheassignedfunctionmustnotbeexecutedinthecurrentthrea
procedureForceDifferentThread;
end;
AsIfancygenericsandanonymousmethodsI'
mhappythatthere'
saTAsyncCallsclassnicelywrappingcallstomyfunctionsIwanttobeexecutedinathreadedmanner.
Here'
sanexamplecalltoamethodexpectingtwointegerparameters(returninganIAsyncCall):
TAsyncCalls.Invoke(AsyncMethod,i,Random(500));
TheAsyncMethodisamethodofaclassinstance(forexample:
apublicmethodofaform),andisimplementedas:
functionTAsyncCallsForm.AsyncMethod(taskNr,sleepTime:
integer):
integer;
begin
result:
=sleepTime;
Sleep(sleepTime);
TAsyncCalls.VCLInvoke(
procedure
begin
Log(Format('
done>
nr:
%d/tasks:
%d/slept:
%d'
[tasknr,asyncHelper.TaskCount,sleepTime]));
end);
Again,I'
musingtheSleepproceduretomimicsomeworkloadtobedoneinmyfunctionexecutedinaseparatethread.
TheTAsyncCalls.VCLInvokeisawaytodosynchronizationwithyourmainthread(application'
smainthread-yourapplicationuserinterface).VCLInvokereturnsimmediately.Theanonymousmethodwillbeexecutedinthemainthread.There'
salsoVCLSyncwhichreturnswhentheanonymousmethodwascalledinthemainthread.
ThreadPoolinAsyncCalls
Asexplainedintheexamples/helpdocument(AsyncCallsInternals-Threadpoolandwaiting-queue):
Anexecutionrequestisaddedtothewaiting-queuewhenanasync.functionisstarted...Ifthemaximumthreadnumberisalreadyreachedtherequestremainsinthewaiting-queue.Otherwiseanewthreadisaddedtothethreadpool.
Backtomy"
task:
whenfeeding(inaforloop)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Delphi Thread Pool Example Using AsyncCalls 异步调用 异步 调用
链接地址:https://www.bdocx.com/doc/15279653.html