var
i,nRet:Integer;
wYear: Word;
wMonth: Word;
wDayOfWeek: Word;
wDay: Word;
wHour: Word;
wMinute: Word;
wSecond: Word;
wMilliseconds: Word;
SysTime: TsystemTime;
begin
  for i:=1 to strtoint(EditTimes.Text) do
  begin
        GetSystemTime(SysTime);
        Caption:=IntToStr(SysTime.wYear)+':'+IntToStr(SysTime.wMonth)+':'+IntToStr(SysTime.wDay)+' '+IntToStr(SysTime.wHour+8)
                +':'+IntToStr(SysTime.wMinute)+':'+IntToStr(SysTime.wSecond)+':'+IntToStr(SysTime.wMilliseconds);
        nRet := sendMessage(inttostr(i));        if nRet > 0 then
        begin
                memo1.Lines.Add(Caption+' 发送成功,任务ID:'+ IntToStr(nRet));
        end
        else
        begin
                memo1.Lines.Add(Caption+' 发送失败,错误码:'+ IntToStr(nRet));
        end;
  end;
end;以上代码是发消息然后写日志到MEMO里,
我想学习用多线程来实现,比如新建5个线程(CreateThread)来发这个消息,这个代码要怎么改?

解决方案 »

  1.   

    建议楼主先去看一下,Delphi目录下有Thread Demo
      

  2.   

    弄好了,function SendThread(P:pointer):Longint;stdcall;
    var
    i,nRet:Integer;
    wYear: Word;
    wMonth: Word;
    wDayOfWeek: Word;
    wDay: Word;
    wHour: Word;
    wMinute: Word;
    wSecond: Word;
    wMilliseconds: Word;
    SysTime: TsystemTime;
    Caption:string;
    begin
      for i:=1 to strtoint(EditTimes.Text) do 
      begin 
            GetSystemTime(SysTime); 
            Caption:=IntToStr(SysTime.wYear)+':'+IntToStr(SysTime.wMonth)+':'+IntToStr(SysTime.wDay)+' '+IntToStr(SysTime.wHour+8) 
                    +':'+IntToStr(SysTime.wMinute)+':'+IntToStr(SysTime.wSecond)+':'+IntToStr(SysTime.wMilliseconds); 
            nRet := sendMessage(inttostr(i));         if nRet > 0 then 
            begin 
                    memo1.Lines.Add(Caption+' 发送成功,任务ID:'+ IntToStr(nRet)); 
            end 
            else 
            begin 
                    memo1.Lines.Add(Caption+' 发送失败,错误码:'+ IntToStr(nRet)); 
            end; 
      end; 
    end;procedure TForm1.Button2Click(Sender: TObject);
    var 
    CThread:Thandle;//声明了一个句柄
    Tid:DWord;
    i:integer;
    begin
      for i:=0 to strtoint(EditThread.Text)-1 do
            Cthread:=CreateThread(nil,0,@SendThread,nil,0,Tid); //创建一个线程,同时调用线程函数end;