Omnithreadlibary學習(2)-IOmniTask異步執行

 1.使用異步

procedure Parallel.Async(task: TProc; taskConfig: IOmniTaskConfig);

匿名委託訪問網站網站

program main;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,
  OtlParallel, OtlTask,
  IdHttp,
  Web.HTTPApp,
  System.classes, WinApi.Windows;

procedure HttpGet(Host, URL: String);
 var
   http:   TIdHttp;
begin
   http := TIdHttp.Create;

  // http.Request.Host := Host;
   http.Request.Method := 'GET';
   http.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36';
   http.Request.AcceptLanguage :='zh-CN,zh;q=0.8';
   http.Get(Host + URL);

   Writeln('===================Response Header==========================');
   Writeln('Server: ' + http.Response.Server);
   Writeln('Status Code :' + http.Response.ResponseCode.ToString);
   Writeln('CharSet:' + http.Response.CharSet);
   Writeln('ContentLength:' + http.Response.ContentLength.ToString());
   Writeln('ContentType:' + http.Response.ContentType);
   http.Free;
end;
begin
  Parallel.Async(
   procedure(const task: IOmniTask)
   begin

      Writeln('Thread ID ' + GetCurrentThreadID.ToString());
      HttpGet('http://zh.wikipedia.org/',  String(HttpEncode('wiki/文檔')));
      //訪問主界面資源使用Invoke Invoke主線程運行, 訪問GUI
     // task.Invoke();
   end
  );

  Parallel.Async(
   procedure(const task: IOmniTask)
   begin
      Writeln('Thread ID ' + GetCurrentThreadID.ToString());
      HttpGet('http://zh.wikipedia.org/',  String(HttpEncode('wiki/文檔')));
      //訪問主界面資源使用Invoke
   //   task.Invoke();
   end
  );

  Parallel.Async(
   procedure(const task: IOmniTask)
   begin
      Writeln('Thread ID ' + GetCurrentThreadID.ToString());
      HttpGet('http://zh.wikipedia.org/',  String(HttpEncode('wiki/文檔')));
      //訪問主界面資源使用Invoke
   //   task.Invoke();
   end
  );
  writeln('異步執行');
  Readln;
end.

相關文章
相關標籤/搜索