我的Image是从资源文件里面读出来的HBITMAP,这些图片就是用户的状态,保存在独自的变量中,通过线程实时的刷新,但是过一会儿这些图片就不显示了,不知道是为什么?
unit uStatusThread;interfaceuses
    Classes, SysUtils, TypInfo, PwClient_TLB, Umain, UResData, IdGlobal, StdCtrls,
    ExtCtrls, Forms, Windows;type
    TStatusThread = class(TThread)
    public
    private
        procedure Refresh;
    public
        Constructor Create();
    protected
        xz: HBITMAP;
        sm: HBITMAP;
        procedure Execute; override;
    end;var
    thdStatus: TStatusThread;implementation{ Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure uStatusThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ uStatusThread }constructor TStatusThread.Create;
begin
    xz := ResData.getBMP('xz');  //从资源文件读取出来的BMP图片
    sm := ResData.getBMP('sm');
    Inherited Create(False);
end;procedure TStatusThread.Execute;
var
    sl: Integer;
begin
    sl := 100;
    while not Terminated do
    begin
        Sleep(sl);
        Application.ProcessMessages;
        //frmMain.UpdateInfoStatus(frmMain.PwClient1.AgentState);
        Synchronize(Refresh);
    end;
end;procedure TStatusThread.Refresh;
var
    aString: TStringList;
    aSubString: TStringList;
    aData: TStringList;
    aCount: Integer;
    cCount: Integer;
    Row: Integer;
    I: Integer;
begin
    I := 0;
    Row := 0;
    cCount := frmMain.ScrollBox2.ControlCount;
    aString := TStringList.Create;
    aData := TStringList.Create;
    try
        aString.Delimiter := ';';
        aString.DelimitedText := frmMain.PwClient1.AgentExtList;
        for aCount := 0 to aString.Count - 1 do
        begin
            aSubString := TStringList.Create;
            try
                aSubString.Delimiter := '|';
                aSubString.DelimitedText := aString.Strings[aCount];
                if aSubString.Count = 7 then
                begin
                    if (aSubString.Strings[6] = '2') or (aSubString.Strings[6] = '3') then
                        Continue;
                    aData.Add(aString.Strings[aCount]);
                    //Inc(Row);
                end;
            finally
                aSubString.Free;
            end;
        end;
    finally
        aString.Free;
    end;
    try
        while I < cCount - 1 do
        begin
            if aData.Count > 0 then
            begin
                aSubString := TStringList.Create;
                try
                    aSubString.Delimiter := '|';
                    aSubString.DelimitedText := aData.Strings[0];
                    (frmMain.ScrollBox2.Controls[I] as TLabel).Caption := aSubString.Strings[2];
                    Inc(I);
                    if aSubString.Strings[6] = '1' then
                        (frmMain.ScrollBox2.Controls[I] as TImage).Picture.Bitmap.Handle := xz
                    else
                    begin
                        (frmMain.ScrollBox2.Controls[I] as TImage).Picture.Bitmap.Handle := sm;
                    end;
                    Inc(I);
                    Inc(I);
                    Inc(Row);
                    aData.Delete(0);
                finally
                    aSubString.Free;
                end;
            end
            else
                Break;
        end;
        if cCount / 3 > row then
        begin
            Dec(cCount);
            while I < cCount do
            begin
                (frmMain.ScrollBox2.Controls[cCount] as TImage).Free;
                Dec(cCount);
                (frmMain.ScrollBox2.Controls[cCount] as TImage).Free;
                Dec(cCount);
                (frmMain.ScrollBox2.Controls[cCount] as TLabel).Free;
                Dec(cCount);
            end;
        end
        else
        begin
            if aData.Count > 0 then
            begin
                aSubString := TStringList.Create;
                try
                    aSubString.Delimiter := '|';
                    aSubString.DelimitedText := aData.Strings[0];
                    if aSubString.Strings[6] = '1' then
                        frmMain.CreateAgentItem(aSubString.Strings[2], xz)
                    else
                    begin
                        frmMain.CreateAgentItem(aSubString.Strings[2], sm)
                    end;
                    aData.Delete(0);
                finally
                    aSubString.Free;
                end;
            end;
        end;    finally
        aData.Free;
    end;
end;end.

解决方案 »

  1.   

    跟踪看看这里:
              while I < cCount do
                begin
                    (frmMain.ScrollBox2.Controls[cCount] as TImage).Free;
                    Dec(cCount);
                    (frmMain.ScrollBox2.Controls[cCount] as TImage).Free;
                    Dec(cCount);
                    (frmMain.ScrollBox2.Controls[cCount] as TLabel).Free;
                    Dec(cCount);
                end;
      

  2.   

    在線程中了,就不要 Application.ProcessMessages 重入了。
         (frmMain.ScrollBox2.Controls[cCount] as TImage).Free; 
                    Dec(cCount); 
                    (frmMain.ScrollBox2.Controls[cCount] as TImage).Free; 
                    Dec(cCount); 
                    (frmMain.ScrollBox2.Controls[cCount] as TLabel).Free; 
                    Dec(cCount);  這裏釋放與上面會不會有什麼處理不對的地方。最近怕深入看代碼。頭疼的難受
      

  3.   

    跟踪了,下面的代码没有执行
    while I < cCount do 
                begin 
                    (frmMain.ScrollBox2.Controls[cCount] as TImage).Free; 
                    Dec(cCount); 
                    (frmMain.ScrollBox2.Controls[cCount] as TImage).Free; 
                    Dec(cCount); 
                    (frmMain.ScrollBox2.Controls[cCount] as TLabel).Free; 
                    Dec(cCount); 
                end;