{"id":852,"date":"2014-10-11T21:38:15","date_gmt":"2014-10-11T13:38:15","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=852"},"modified":"2014-10-13T14:01:11","modified_gmt":"2014-10-13T06:01:11","slug":"%e7%a4%ba%e4%be%8b-%e4%bd%bf%e7%94%a8qworker%e4%bb%8e%e7%99%be%e5%ba%a6%e5%a4%a9%e6%b0%94%e5%b9%b6%e8%a1%8c%e8%8e%b7%e5%8f%96%e5%a4%a9%e6%b0%94%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=852","title":{"rendered":"\u793a\u4f8b-\u4f7f\u7528QWorker\u4ece\u767e\u5ea6\u5929\u6c14\u5e76\u884c\u83b7\u53d6\u5929\u6c14\u6570\u636e"},"content":{"rendered":"<p>\u4eca\u5929\u65b0\u589e\u4e86\u4e00\u4e2a\u4f7f\u7528WeatherFetch\u7684\u4f8b\u5b50\uff0c\u7b80\u5355\u7684\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528TQJobGroup\u548cTQForJobs\u6765\u5e76\u884c\u4ece\u767e\u5ea6\u5929\u6c14\u83b7\u53d6\u6570\u636e\u3002\u672c\u6587\u4e3a\u60a8\u7565\u52a0\u89e3\u8bfb\u76f8\u5173\u4ee3\u7801\uff0c\u4ee5\u4fbf\u8ba9\u5927\u5bb6\u66f4\u6613\u4e8e\u7406\u89e3\u8fd9\u4e00\u793a\u4f8b\u3002\u8fd9\u4e2a\u4f8b\u5b50\u4e3b\u8981\u6f14\u793a\u4e86\uff1a<\/p>\n<p>1\u3001QWorker \u4e2d\u4f7f\u7528 TQJobGroup \u5b9a\u5236\u987a\u5e8f\u6267\u884c\u4f5c\u4e1a\u6d41\u7a0b\u3002<\/p>\n<p>2\u3001QWorker \u4e2d TQForJobs \u4e2d For \u5e76\u884c\u7f16\u7a0b\u7684\u4f7f\u7528\u3002<\/p>\n<p>3\u3001\u4f7f\u7528 Windows WinINet API \u6765\u8bbf\u95ee\u7f51\u7ad9\u7684\u65b9\u5f0f\u3002<\/p>\n<p><strong>\u3010\u6ce8\u610f\u3011<\/strong><\/p>\n<p><strong>1\u3001\u53d7\u767e\u5ea6API\u7684\u9650\u5236\uff0c\u8fd9\u4e2a\u4f8b\u5b50\u53ea\u80fd\u6bcf\u5929\u67e5\u8be25000\u6b21\u5929\u6c14\u4fe1\u606f\uff0c\u8d85\u8fc7\u540e\u5c06\u65e0\u6cd5\u83b7\u53d6\u5230\u6b63\u786e\u7684\u6570\u636e\u3002<\/strong><br \/>\n<strong>2\u3001\u5982\u679c\u4f60\u4f7f\u7528\u4ee3\u7406\u4e0a\u7f51\uff0c\u8bf7\u4fee\u6539InternetOpen\u51fd\u6570\u7684\u53c2\u6570\uff0c\u5c06\u4ee3\u7406\u670d\u52a1\u5668\u53c2\u6570\u8bbe\u7f6e\u597d\uff0c\u5426\u5219\u5c06\u540c\u6837\u83b7\u53d6\u4e0d\u5230\u5929\u6c14\u4fe1\u606f\u3002<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u672c\u793a\u4f8b\u662f\u4e00\u4e2a\u4e24\u6b65\u5206\u7ec4\u4f5c\u4e1a\uff1a<\/p>\n<p>1\u3001\u4f7f\u7528For\u5e76\u884c\u6765\u4ece\u767e\u5ea6\u83b7\u53d6147\u4e2a\u57ce\u5e02\u7684\u5929\u6c14\u9884\u62a5\u4fe1\u606f\u3002<\/p>\n<p>2\u3001\u5b8c\u6210\u540e\uff0c\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u62a5\u544a\u4f5c\u4e1a\u82b1\u8d39\u7684\u65f6\u95f4\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u6765\u5148\u770b\u5206\u7ec4\u4f5c\u4e1a\u7684\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:delphi decode:true\">procedure TForm4.Button1Click(Sender: TObject);\r\nvar\r\n  AGroup: TQJobGroup;\r\nbegin\r\nButton1.Enabled := False;\r\nButton1.Caption := '\u83b7\u53d6\u4e2d...';\r\nMemo1.Lines.Clear;\r\nAGroup := TQJobGroup.Create(True);\r\nAGroup.Prepare;\r\nAGroup.Add(DoFetchWeathers, nil, False);\r\nAGroup.Add(DoShowResult, Pointer(GetTickCount), True);\r\nAGroup.Run();\r\nAGroup.MsgWaitFor();\r\nFreeObject(AGroup);\r\nButton1.Caption := '\u5f00\u59cb';\r\nButton1.Enabled := True;\r\nend;<\/pre>\n<p>\u5728\u8fd9\u4e2a\u51fd\u6570\u4e2d\uff0c\u6211\u4eec\u505a\u4e86\u51e0\u4ef6\u4e8b\uff1a<\/p>\n<p>\uff081\uff09\u5148\u5c06 Button1 \u7684\u6309\u94ae\u7ed9\u7981\u7528\uff0c\u4ee5\u907f\u514d\u591a\u6b21\u70b9\u51fb\u3002<\/p>\n<p>\uff082\uff09\u4f7f\u7528 TQJobGroup.Create(True) \u521b\u5efa\u4e86\u4e00\u4e2a\u4e32\u884c\u5316\u7684\u4f5c\u4e1a\u5e8f\u5217\uff0c\u5f53 TQJobGroup \u7684\u6784\u9020\u51fd\u6570\u53c2\u6570\u4e3aTrue\u65f6\uff0c\u6dfb\u52a0\u5230\u5206\u7ec4\u4e2d\u7684\u4f5c\u4e1a\u5c06\u4e25\u683c\u6309\u7167\u4f5c\u4e1a\u52a0\u5165\u7684\u5148\u540e\u987a\u5e8f\u800c\u4e0d\u662f\u5e76\u884c\u6267\u884c\uff0c\u56e0\u4e3a\u6211\u4eec\u7b2c\u4e8c\u6b65\u8981\u7b49\u5f85\u7b2c\u4e00\u6b65\u5b8c\u6210\u624d\u9700\u8981\u6267\u884c\uff0c\u6240\u4ee5\u6211\u4eec\u53c2\u6570\u88ab\u8bbe\u7f6e\u4e3a True \u3002\u6211\u4eec\u5728\u8bbe\u7f6e\u4e32\u884c\u5316\u6267\u884c\u7684\u4f5c\u4e1a\u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u8fd9\u4e00\u70b9\u3002<\/p>\n<p>\uff083\uff09\u4ece AGroup.Prepare \u5f00\u59cb\u5230 AGroup.MsgWaitFo r\u5b8c\u6210\u4f5c\u4e1a\u6dfb\u52a0\u5e76\u6267\u884c\u7b49\u5f85\u7684\u8fc7\u7a0b\uff0c\u8fd9\u5176\u4e2d\uff0cPrepare\u548cRun\u5e76\u4e0d\u662f\u5fc5\u9700\u7684\uff0c\u53ea\u662f\u5728\u6279\u91cf\u6dfb\u52a0\u4f5c\u4e1a\u65f6\uff0c\u8fd9\u6837\u505a\u66f4\u9ad8\u6548\u4e00\u4e9b\u3002\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86MsgWaitFor\u800c\u4e0d\u662fWaitFor\u4ee5\u907f\u514d\u4e3b\u754c\u9762\u6b7b\u6389\uff0c\u56e0\u4e3a\u4f5c\u4e1a\u7684\u7b2c\u4e8c\u6b65\u662f\u5728\u4e3b\u7ebf\u7a0b\u4e2d\u6267\u884c\uff0c\u5982\u679c\u4f60\u4f7f\u7528WaitFor\u4f1a\u7531\u4e8e\u4e3b\u7ebf\u7a0b\u6b7b\u6389\u800c\u9677\u5165\u6b7b\u9501\u3002\u8fd9\u4e00\u70b9\u5728\u7b49\u5f85\u4f5c\u4e1a\u5b8c\u6210\u65f6\u4e00\u5b9a\u8981\u6ce8\u610f\u3002<\/p>\n<p>\uff084\uff09\u4f5c\u4e1a\u5b8c\u6210\u7684\u6e05\u7406\u5de5\u4f5c\uff0c\u91ca\u653e\u521b\u5efa\u7684\u5206\u7ec4\u5bf9\u8c61\uff0c\u6062\u590d\u6309\u94ae\u72b6\u6001\u3002<\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u5b89\u6392\u4e86\u4e24\u4e2a\u4f5c\u4e1a\uff0cDoFetchWeathers \u548c DoShowResult\uff0c\u6211\u4eec\u73b0\u5728\u6765\u5206\u522b\u770b\u5b83\u4eec\u7684\u6e90\u7801\uff1a<\/p>\n<pre class=\"lang:delphi decode:true\">procedure TForm4.DoFetchWeathers(AJob: PQJob);\r\nbegin\r\nTQForJobs.For(0, 147, FetchCityWeather);\r\nend;\r\n\r\nprocedure TForm4.DoShowResult(AJob: PQJob);\r\nbegin\r\nShowMessage('\u83b7\u53d6\u5929\u6c14\u6570\u636e\u5b8c\u6210\uff0c\u7528\u65f6' + Rolluptime((GetTickCount - Cardinal(AJob.Data))\r\n  div 1000));\r\nend;<\/pre>\n<p>\u5f88\u7b80\u5355\uff0c DoFetchWeather\u4f7f\u7528For\u5e76\u884c\u8ba1\u7b97\u6765\u8c03\u7528FetchCityWeather\u51fd\u6570\u6765\u83b7\u53d6\u8fd9148\u4e2a\u57ce\u5e02\u7684\u5929\u6c14\u6570\u636e\uff0cDoShowResult \u76f4\u63a5\u663e\u793a\u4e86\u83b7\u53d6\u5929\u6c14\u6570\u636e\u7684\u7528\u65f6\u3002<\/p>\n<p>\u6211\u4eec\u8fdb\u4e00\u6b65\u770b\u4e0bFetchCityWeather\u51fd\u6570\u7684\u4ee3\u7801\uff1a<\/p>\n<pre class=\"lang:delphi decode:true\">procedure TForm4.FetchCityWeather(ALoopMgr: TQForJobs; AJob: PQJob;\r\n  AIndex: NativeInt);\r\nvar\r\n  AConn, AHttp: HINTERNET;\r\n  AReaded: Cardinal;\r\n  S: TMemoryStream;\r\n  AJson: TQJson;\r\n  AWeather: PString;\r\n  ABuf: array [0 .. 65535] of Byte;\r\n  function EncodeUtf8Url(S: QStringW): QStringW;\r\n  var\r\n    U8: QStringA;\r\n    p: PQCharA;\r\n  begin\r\n  U8 := QString.Utf8Encode(S);\r\n  p := PQCharA(U8);\r\n  SetLength(Result, 0);\r\n  while p^ &lt;&gt; 0 do\r\n    begin\r\n    if p^ &lt; 128 then\r\n      begin\r\n      Result := Result + Char(p^);\r\n      Inc(p);\r\n      end\r\n    else\r\n      begin\r\n      while p^ &gt;= 128 do\r\n        begin\r\n        Result := Result + '%' + IntToHex(p^, 2);\r\n        Inc(p);\r\n        end;\r\n      end;\r\n    end;\r\n  end;\r\n\r\nbegin\r\nAConn := InternetOpen(nil, INTERNET_OPEN_TYPE_DIRECT, nil, nil, 0);\r\nif AConn &lt;&gt; nil then\r\n  begin\r\n  AHttp := InternetOpenUrl(AConn,\r\n    PChar('http:\/\/api.map.baidu.com\/telematics\/v3\/weather?location=' +\r\n    EncodeUtf8Url(Cities[AIndex]) +\r\n    '&amp;output=json&amp;ak='+BaiduAK), nil, 0, 0, 0);\r\n  if AHttp &lt;&gt; nil then\r\n    begin\r\n    S := TMemoryStream.Create;\r\n    try\r\n      while InternetReadFile(AHttp, @ABuf[0], 65536, AReaded) and\r\n        (AReaded &gt; 0) do\r\n        S.WriteBuffer(ABuf[0], AReaded);\r\n      S.Position := 0;\r\n      AJson := TQJson.Create;\r\n      try\r\n        if AJson.TryParse(LoadTextW(S)) then\r\n          begin\r\n          \/\/ \u66f4\u65b0\u5929\u6c14\u4fe1\u606f\r\n          New(AWeather);\r\n          AWeather^ := IntToStr(AIndex) + '-' + Cities[AIndex] + '\u5929\u6c14:'#13#10 +\r\n            AJson.AsJson;\r\n          Workers.Post(DoLogWeather, AWeather, True);\r\n          end;\r\n      finally\r\n        FreeObject(AJson);\r\n      end;\r\n    finally\r\n      FreeObject(S);\r\n      InternetCloseHandle(AHttp);\r\n    end;\r\n    end;\r\n  InternetCloseHandle(AConn);\r\n  end;\r\nend;<\/pre>\n<p>\u4e00\u4e2a\u57fa\u672c\u6807\u51c6\u7684\u4f7f\u7528 WinInet \u51fd\u6570\u53d1\u9001 HTTP GET\u8bf7\u6c42\u5e76\u83b7\u53d6\u7ed3\u679c\u7684\u8fc7\u7a0b\uff0c\u8fd9\u91cc\u56e0\u4e3a\u662f\u6f14\u793a\u7684\u4f8b\u5b50\uff0c\u6211\u6ca1\u592a\u5904\u7406\u51fa\u9519\u7684\u95ee\u9898\u3002\u83b7\u53d6\u5230\u7ed3\u679c\u540e\uff0c\u6211\u4eec\u89e3\u6790\u6536\u5230\u7684 Json \u683c\u5f0f\u5929\u6c14\u6570\u636e\uff0c\u7136\u540e\u89e6\u53d1\u4e86\u4e00\u4e2a\u4e3b\u7ebf\u7a0b\u5f02\u6b65\u4f5c\u4e1a DoLogWeather \u6765\u5c06\u5f97\u5230\u7684Json\u7ed3\u679c\u8bb0\u5f55\u5230 Memo \u4e2d\u663e\u793a\u51fa\u6765\u3002\u8fd9\u91cc\u6211\u6ca1\u6709\u7279\u610f\u4fdd\u8bc1AWeather\u91ca\u653e\u7684\u95ee\u9898\uff0c\u5b9e\u9645\u4e0a\u5982\u679c\u5728\u4f5c\u4e1a\u672a\u5b8c\u6210\u524d\u91ca\u653e\u53ef\u80fd\u4f1a\u5b58\u5728\u5185\u5b58\u6cc4\u6f0f\uff0c\u66f4\u7406\u60f3\u7684\u65b9\u5f0f\u662f\u76f4\u63a5\u4f20\u9012 AJson \u5bf9\u8c61\uff0c\u7136\u540e\u8bbe\u7f6e jdfFreeAsObject \u3002<\/p>\n<p><strong>\u3010\u6ce8\u610f\u3011\u6f14\u793a\u7a0b\u5e8f\u4e2d\u7528\u5230\u7684BaiduAK\u662fQDAC\u7533\u8bf7\u7684\u767e\u5ea6\u5f00\u53d1\u8005\u5e94\u7528AK\uff0c\u67095000\u6b21\u8bbf\u95ee\u7684\u914d\u7f6e\u9650\u5236\uff0c\u5982\u679c\u8fd0\u884c\u6f14\u793a\u7684\u7528\u6237\u8fc7\u591a\uff0c\u5f88\u5feb\u5c06\u8d85\u8fc7\u914d\u989d\uff0c\u9020\u6210\u65e0\u6cd5\u8bbf\u95ee\u3002<\/strong><\/p>\n<p>\u6700\u540e\u518d\u770b\u4e0b DoLogWeather \u7684\u6e90\u7801\u5427\uff0c\u6ca1\u5565\u73a9\u610f\u3002<\/p>\n<pre class=\"lang:delphi decode:true \">procedure TForm4.DoLogWeather(AJob: PQJob);\r\nbegin\r\nMemo1.Lines.Add(PString(AJob.Data)^);\r\nDispose(PString(AJob.Data));\r\nend;<\/pre>\n<p>\u597d\u4e86\uff0c\u6574\u4e2a\u4f8b\u5b50\u5230\u4e9b\u7ed3\u675f\uff0c\u6211\u4eec\u770b\u4e00\u4e2a\u7a0b\u5e8f\u6267\u884c\u7684\u6548\u679c\u5427\uff1a<\/p>\n<p><a href=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/10\/weatherfetch.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-853\" src=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/10\/weatherfetch-300x174.jpg\" alt=\"weatherfetch\" width=\"300\" height=\"174\" srcset=\"https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/10\/weatherfetch-300x174.jpg 300w, https:\/\/blog.qdac.cc\/wp-content\/uploads\/2014\/10\/weatherfetch.jpg 628w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h4>\u3010\u5b8c\u6574\u6e90\u7801\u4e0b\u8f7d\u3011\u00a0<a href=\"http:\/\/pan.baidu.com\/s\/1sjkCbox\" target=\"_blank\">\u767e\u5ea6\u7f51\u76d8<\/a><\/h4>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u65b0\u589e\u4e86\u4e00\u4e2a\u4f7f\u7528WeatherFetc [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[69,8,9,6],"tags":[95,98,97,96],"class_list":["post-852","post","type-post","status-publish","format-standard","hentry","category-c-builder","category-delphi","category-qdac","category-qworker","tag-for","tag-98","tag-97","tag-96"],"views":7707,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=852"}],"version-history":[{"count":5,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/852\/revisions"}],"predecessor-version":[{"id":886,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/852\/revisions\/886"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}