Delphi线程的初级应用

viewRadio_th线程函数在form外生命全局变量、函数内相应的局部变量可以接收全局变量的赋值进行操作、query等可以自行创建进行查询、这样结果不会改变。

//下面是后台发送字幕的线程函数应用。

……

var
  ldate,ldatetime:string;
  lHandle:thandle;
  dloopThreadID:dword;
  message_form: Tmessage_form;
  function viewRadio_th(P:pointer):Longint;stdcall;

implementation
uses commonDM, ubox_c, common_u, loginform;

……

//调用线程

lHandle := CreateThread(nil,0,@viewRadio_th,nil,0,dloopThreadID);

//线程函数

function viewRadio_th(P:pointer):Longint;stdcall;

var lquery,lnumquery:Toraquery;
    I,j: Integer;
    th_date,th_datetime:string;
    th_handle:thandle;
    th_loopThreadID:dword;
    maxnum,boxid,boxip:string;
    F:Textfile;
begin
  th_date:=ldate;
  th_datetime:=ldatetime;
  th_handle:=lhandle;
  th_loopThreadID:=dloopThreadID;
  lquery:=Toraquery.Create(nil);
  lnumquery:=toraquery.Create(nil);
  lquery.FetchAll:=true;
  lnumquery.Session:=common_dm.common_session;
  lquery.Session:=common_dm.common_session;
  with lnumquery do
  begin
    close;
    sql.Clear;
    sql.Add('select max(promotesalesplaynumber) maxnum from promotesales_t where PROMOTESALESTYPE=''?¤??'' and '''+
    th_date+' ''||PROMOTESALESPLAYTIME='''+th_datetime+'''');
    open;
  end;    // with
  if lnumquery.RecordCount<>0 then
    maxnum:=lnumquery['maxnum'];
  with lquery do
  begin
    close;
    sql.Clear;
    sql.Add('select * from promotesales_t where PROMOTESALESTYPE=''?¤??'' and '''+
    th_date+' ''||PROMOTESALESPLAYTIME='''+th_datetime+'''');
    open;
  end;    // with
  for I := 1 to strtoint(maxnum) do    // Iterate
  begin
    lquery.First;
    if i<>1 then
      sleep(60000);
    for j := 0 to lquery.RecordCount - 1 do    // Iterate
    begin
      if strtoint(lquery['promotesalesplaynumber'])>=i then
      begin
        boxid:=lquery['promoteboxid'];
        with lnumquery do
        begin
           Close;
           SQL.Clear;
           SQL.Add('select * from BOX_T where BOX'+boxid+'''');
           Open;
        end;    // with
        boxip:=lnumquery['boxip'];
        if fileExists(common_u.fileaddress(boxip,'radioroot')+'.tit') then
          Deletefile(common_u.fileaddress(boxip,'radioroot')+'.tit');
        AssignFile(F, common_u.fileaddress(boxip,'radioroot')+'.tit'); {??????????±??? F ????}
        ReWrite(F);
        Writeln(F, '[Parameter]'+#13+'Delay=0'+#13+'IntoDelay=0'+#13+#13+'[TextAdOsd]'+
        #13+'PlayMode=RIGHT_IN_LEFT_OUT'+#13+'Type=Text'+#13+'Font=????'+#13+'FontSize=40'+#13+'LoopTime=1'+#13+'Speed=5'
        +#13+'Delay=0'+#13+'Time=0'+#13+'ForegroundColor=255 255 0'+#13+'BackgroundColor=0 0 0'+#13+'UseShadow=y'+#13+'ShadowColor=255 0 0'
        +#13+'ShadowDelta=1 1'+#13+'cmd=first'+#13+'Text='+lquery['PROMOTESALESCONTENT']);
        Closefile(F); {??±????? F}
      end;
      lquery.Next;
    end;    // for

  end;    // for
  lquery.Free;
  lnumquery.Free;
  if th_handle<>0 then
    TerminateThread(th_handle,th_loopThreadID);
end;