zoukankan      html  css  js  c++  java
  • Omnithreadlibary学习(3)-IOmniTask异步执行SendMessage

    在任务中发送消息, 可以是函数或者对象方法

      TOmniTaskMessageEvent = procedure(const task: IOmniTaskControl; const msg: TOmniMessage) of object; 
      TOmniOnTerminatedFunction = reference to procedure(const task: IOmniTaskControl);
    program main;
    
    {$APPTYPE CONSOLE}
    
    {$R *.res}
    
    uses
      System.SysUtils,
      System.Classes,
      OtlParallel,
      OtlTask,
      OtlTaskControl,
      OtlComm, OtlCommon,
      IdHttp,
      Web.HTTPApp,
      WinApi.Windows,
      WinApi.Messages;
    
    const
      WM_RESULT = WM_USER;
    
    
    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;
    
    
    procedure LogResult(const task: IOmniTaskControl; const msg: TOmniMessage);
    begin
      WriteLn(Format('异步GET: %s',
      [msg.MsgData.CastToStringDef('')]));
    end;
    
    begin
     {异步执行发送消息}
      Parallel.Async(
       procedure(const task: IOmniTask)
       var
        Msg: String;
       begin
          Writeln('Thread ID ' + GetCurrentThreadID.ToString());
          Msg := 'task1 Start';
          task.Comm.Send(WM_RESULT,  Msg);
          HttpGet('http://zh.wikipedia.org/',  String(HttpEncode('wiki/文档')));
          task.Comm.Send(WM_RESULT,  'task1 completed');
       end, Parallel.TaskConfig.Onmessage(WM_RESULT, LogResult));
      Readln;
    end.

    1.消息没有接收到..奇怪了

  • 相关阅读:
    DM数据库disql的使用 Disql disql 达梦数据库Disql
    移动端禁止蒙层下的页面滚动
    移动端如何自动适配px
    使用Vant做移动端对图片预览ImagePreview和List的理解
    uniapp中使用uView组件库
    h5使用vuephotopreview 做全屏预览
    jsonview的实现
    PC端自适应使用rem 移动端适配升级版
    axios解决跨域问题(vuecli3.0)
    vs code 配置git path
  • 原文地址:https://www.cnblogs.com/pengshaomin/p/3910633.html
Copyright © 2011-2022 走看看