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.消息沒有接收到..奇怪了函數

相關文章
相關標籤/搜索