首页 > 代码库 > Omnithreadlibary学习(2)-IOmniTask异步执行

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.