在线程中更新窗体上的ListView控件上的内容,更新时如果不使用Synchronize(更新函数)就会抱错Code:1400,使用的话又会导致界面无响应

解决方案 »

  1.   

    我也在线程中用LISTVIEW,没出现过,你把代码贴出来看看
      

  2.   

    TDisposeFileThread = class(TThread)
      private
        fSender             :THandle;//发送窗口句柄
        fWorkStatus         :Boolean;//线程运行状态,要求声明为公用
        fListViewDisplay    :TListView;//处理文件,转换完成后的显示区域,现在使用ListView
        fListViewSrc        :TListView;//文件的原始信息,这个是不可见的
        fFile               :string;//传入的文件
        fReadText           :WideString;//每次读取的文件内容
        fFieldList          :TFieldList;//传入文件的格式,以及那些字段需要进行转换
      protected
        procedure Execute; override;
      public
        property WorkStatus:Boolean read fWorkStatus  write fWorkStatus;
        constructor Create(hSender,hhint:THandle;LVDisplay,LVSrc:TListView;const FileName:String;FieldList:TFieldList);
        destructor Destroy();
        procedure AddItem;
        function ConvertText(const nType:ShortInt;szText:String):string;
        function ConvertDateTimeField(const szField:String):String;
        function ConvertMoneyFen2Yuan(const szMoney:String):String;
        function SplitStr(SzValue:WideString;var retList:TStringList):Boolean;
        function ConvertCtrlCode(const nCtrlItemType:ShortInt;szText:String):string;
      end;
      
    implementation
    procedure TDisposeFileThread.AddItem;
    var
      j,i,MinIndex,MaxIndex:Shortint;
      SplitList:TStringList;
      ConvText :string;
      bIsCaption:Boolean;
    begin
      SplitList:=TStringList.Create;
      SplitStr(fReadText,SplitList);
      MinIndex:= Low(fFieldList);
      MaxIndex:=High(fFieldList);
      with fListViewDisplay.Items.Add do
      begin
        bIsCaption:=True;
        for i:=MinIndex to MaxIndex do
        begin
          if (fFieldList[i].nVisible=1)then
          begin
            ConvText:=  ConvertText(fFieldList[i].nType,SplitList.Strings[fFieldList[i].nPosInFile-1]);
            if bIsCaption then
            begin
              Caption :=  ConvText;
              bIsCaption:=False;
            end
            else
              SubItems.Add(ConvText);
          end
        end;
      end;
      with fListViewSrc.Items.Add do
      begin
        ConvText:=  SplitList.Strings[fFieldList[MinIndex].nPosInFile];
        Caption :=  ConvText;
        for i:=MinIndex+1 to MaxIndex do
        begin
          ConvText:=SplitList.Strings[fFieldList[i].nPosInFile-1];
          SubItems.Add(ConvText);
        end;
      end;
      SplitList.Free;
    end;
    constructor TDisposeFileThread.Create(hSender,hHInt:THandle;LVDisplay,LVSrc:TListView;const FileName:String;FieldList:TFieldList);
    begin
      inherited Create(True);
      FreeOnTerminate:=True;
      fSender         :=hSender;
      fhint           :=hhint;
      fListViewDisplay:=LVDisplay;
      fListViewSrc    :=LVSrc;
      fFieldList      :=FieldList;
      fFile           :=FileName;
      WorkStatus      :=True;
    end;procedure TDisposeFileThread.Execute;
    label
      OnExit;
    var
      f:TextFile;
      wTemp:Widestring;
      I:ShortInt;
    begin
      LockWindowUpdate(fListViewDisplay.Handle);
      fListViewDisplay.Items.Clear;
      fListViewSrc.Items.Clear;
      LockWindowUpdate(0);
      if not FileExists(fFile) then
      goto OnExit;
      AssignFile(f,fFile);
      Reset(f);
      i:=0;
      {
      while not Terminated do
      begin
        if WorkStatus then
        begin
          while (not Eof(f))and (i<ReadTimesOnce) do
          begin
            Readln(f,wtemp);
            fReadList.Add(wTemp);
            Inc(i);
          end;
          Synchronize(AddItem);
          Sleep(1);
        end
        else
        begin
          fListViewDisplay.Items.Clear;
          fListViewSrc.Items.Clear;
          Break;
        end;  end;
      }
      while (Not Terminated)and (not Eof(f)) do
      begin
        if (WorkStatus) then
        begin
          Readln(f,wTemp);
          fReadText:=wTemp;
          Synchronize(AddItem);
        end
        else
        begin
          fListViewDisplay.Items.Clear;
          fListViewSrc.Items.Clear;
          Break;
        end;
      end;
      CloseFile(f);
    OnExit:
      SendMessage(fSender,WM_DOTRANMSG,0,0);
    end;