procedure TMainFrm.XTConnection;
var
  Recnum1:array of string;
  AShape:Array[0..75] of TShape;
  hy :Double;
  i: integer;
  s3, s1,s2:TDateTime;
  date :String;
begin
  setlength(recnum1,75);
  Query2.Close;
  Query2.SQL.Text :='select Max(recnum)as recnum,line_num,tower_num from history_data where isolate_num='+'0001'+ 'group by line_num,tower_num';
  Query2.Open;
  Query2.First;
  for i:=0 to Query2.RecordCount -1 do
  begin
    Recnum1[i] :=inttostr(Query2.fieldbyname('recnum').AsInteger);
    Query2.Next;
  end;  setlength(recnum1,75);
  for i :=Low(Recnum1) to High(Recnum1) do
  begin
    Query3.Close;
    Query3.SQL.Text :='SELECT c.* FROM line a INNER JOIN'+
                      ' tower b ON a.line_num = b.line_num INNER JOIN'+
                      ' history_data c ON a.line_num = c.line_num AND b.tower_num = c.tower_num AND'+
                      ' isolate_num='+'0001'+'and  c.recnum =' + QuotedStr(recnum1[i]);
    Query3.Open;
    s1:=StrToDateTime(formatdatetime('yyyy-mm-dd',now));
    s2:=StrToDateTime(formatdatetime('hh:mm:ss',time));
    s3:=s1+s2;
    date :=FormatDateTime('yyyy-mm-dd hh:mm:ss', s3);
    DateRQ:=DateTimeToStr(Query3.fieldbyname('riqi').AsDateTime);    if (StrToDateTime(date) - StrToDateTime(DateRQ))*24 >48  then
    begin
      if i<5 then
      TShape(FindComponent('Shape'+inttostr(i))).Brush.Color :=clRed;
    end;
    hy :=(StrToDateTime(date) - StrToDateTime(DateRQ))*24;
    if ( hy > 24) and (hy <= 48) then
    begin
      if i<5 then
      TShape(FindComponent('Shape'+inttostr(i))).Brush.Color :=clYellow;
    end;
  end;
end;
这个过程是在FormCreate事件中执行,显的非常的慢,请各位帮帮忙,有没有什么好的优化方法,在线感谢

解决方案 »

  1.   

    这个和数组没什么关系,主要时间都花在了数据库的查询上,至于你说的循环王数组里面赋值其实这个过程并不占用多少时间,因为这个时候数据已经都被查询到了内存当中保存了
    我觉得你应该从查询这块入手,看看这些数据是不是必须在程序已开始就要读取近来,能不能放到后面进行?如果真的不能避免的话,就做一个启动窗口吧(就像delphi刚刚打开时显示的启动窗口一样,如果时间不是太长,用户应该能够忍受)