• 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.
    

  • 相关阅读:
    square(正方形)
    敌兵布阵
    Addition Chains(加法链)
    贪心算法理论
    难题,未解决
    Linux上vi(vim)编辑器使用教程
    linux下md5sum的使用
    Java读取csv文件
    S2JDBCタイプセーフAPI
    S2JDBCチュートリアル
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/3910500.html
Copyright © 2020-2023  润新知