how do i update listview item index inside thread

后端 未结 1 1168
一个人的身影
一个人的身影 2020-12-20 03:07

i am creating project that allow multi users to login and add there details inside listview but i am stuck with problem , but First here is my threading code wi

相关标签:
1条回答
  • 2020-12-20 03:36

    Try something more like this:

    type
      TDownloadImageReadyEvent = procedure(Sender: TObject; AUserData: Pointer; var AImage: TGifImage) of object;
    
      TDownloadImage = class(TThread)
      private
        FURL: String;
        FGif: TGifImage;
        FOnImageReady: TDownloadImageReadyEvent;
        FUserData: Pointer;
        procedure DoImageReady;
      protected
        procedure Execute; override;
      public
        constructor Create(const AUrl: String; AOnImageReady: TDownloadImageReadyEvent; AUserData: Pointer); reintroduce;
      end;
    
    constructor TDownloadImage.Create(const AUrl: String; AOnImageReady: TDownloadImageReadyEvent; AUserData: Pointer);
    begin
      inherited Create(False);
      FreeOnTerminate := True;
      FUrl := AUrl;
      FOnImageReady := AOnImageReady;
      FUserData := AUserData;
    end;
    
    procedure TDownloadImage.Execute;
    var
      aMs: TMemoryStream;
      aIdHttp: TIdHttp;
    begin
      FGif := TGifImage.Create;
      try
        aMs := TMemoryStream.Create;
        try
          aIdHttp := TIdHttp.Create(nil);
          try
            aIdHttp.Request.UserAgent := 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0';
            aIdHttp.Get(FURL, aMs);
          finally
            aIdHttp.Free;
          end;
          aMs.Position := 0;
          FGif.LoadFromStream(aMs);
          FGif.Transparent := True;
        finally
          aMs.Free;
        end;
        if Assigned(FOnImageReady) then
          Synchronize(DoImageReady);
        end;
      finally
        FGif.Free;
      end;
    end;
    
    procedure TDownloadImage.DoImageReady;
    begin
      if Assigned(FOnImageReady) then
        FOnImageReady(Self, FUserData, FGif);
    end;
    

    procedure TForm1.Add_Item(const strCaption, strFile, strUniqueID: String);
    var
      Item: TListItem;
    begin
      Item := ListView1.Items.Add;
      Item.Caption := '';
      Item.SubItems.Add(strCaption); // subitem 0
      Item.SubItems.Add('IMA'); // subitem 1
      Item.SubItems.Add(strUniqueID); // subitem 2 // Client id
      Item.SubItems.Add('-'); // subitem 3 // Next User Idx (beside)
      Item.Data := nil;
      TDownloadImage.Create(strFile, ImageReady, Item);
    end;
    
    procedure TForm1.ListView1Deletion(Sender: TObject; Item: TListItem);
    begin
      TGifImage(Item.Data).Free;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      Strname, image, strUniqueID: String;
    begin
      Strname := 'Matrin';
      image := ...;
      strUniqueID := ...;
      Add_Item(Strname, image, strUniqueID);
    end;
    
    procedure TForm1.ImageReady(Sender: TObject; AUserData: Pointer; var AImage: TGifImage);
    var
      Item: TListItem;
      i: Integer;
      sClientID: string;
    begin
      Item := TListItem(AUserData);
    
      if ListView1.Items.IndexOf(Item) = -1 then
        Exit;
    
      Item.Data := AImage;
      AImage := nil;
    
      if Streamin then
      begin
        sClientID := IntToStr(IDCLIENT);
        for i := 0 to ListView1.Items.Count - 1 do
        begin
          if ListView.Items[i].SubItems[3] = sClientID then
          begin
            ExchangeItems(ListView1, Item.Index, 0);
            Exit;
          end;
        end;
      end;
    end;
    
    0 讨论(0)
提交回复
热议问题