TREEVIEW与LISTVIEW同时使用,当LISTVIEW处理很多数据的时候,我想用线程来做.
但是看了一些资料觉得都将的不怎么详细.
对线程还不怎么理解,
谁有答案呢?

解决方案 »

  1.   

    bluemeteor(挂月||╭∩╮(︶︿︶)╭∩╮) 
    嘻~简介是不是夸张了点呀,对线程序还是不怎么理解,
    :(goomoo(古木)
    就是选中TREEVIEW中一个节点,然后LISTVIEW中的内容也跟着变化(是从库中读取的),当处理大数据量的时候就会很慢,然后什么也不能点,
    我想用线程实现
    DO YOU KNOW?
      

  2.   

    ◎通知(/创建)线程开始处理并增加item到链表(处理时必须等待mutex/cs)
    ◎现场处理完(/一段落)调用同步方法通知treeview开始update(release mutex)
    ◎update前必须等待mutex/cs,update完后release mutex
    ◎线程可继续等待mutex直到处理完。
      

  3.   

    unit ULoadThread;interfaceuses
      Classes,comctrls,Sysutils,comobj;type
      LoadThread = class(TThread)
      private
        { Private declarations }
      protected
        procedure Execute; override;
      end;implementation
    Uses UFrmmain,Ufrmwait;procedure LoadThread.Execute;
    var
      sql : string;
      rs_1,rs_2 : variant;
      Node : TTreeNode ;
      i : Integer;
      item_1 : TListItem;
    begin
      FreeOnTerminate := True ;
      Node := frmmain.tv_node.Selected;
      rs_1 := CreateOleObject('AdoDb.Recordset');
      rs_1.cursorlocation := 3;
      rs_2 := CreateOleObject('AdoDb.Recordset');
      rs_2.cursorlocation := 3;
      frmmain.lv_info.Items.Clear;  if node.Level = 0 then
      begin
        sql := 'select * from helpinfo where left(typ_code,2)  = (select typ_code from helptype where typ_name = '''+trim(node.Text)+'''  and node = ''1'') order by convert(int,hlp_code)';
        rs_1.open(sql,cnmain,0,1);
        if rs_1.recordcount > 0 then
        begin
          for i := 1 to rs_1.recordcount do
          begin
            if Terminated then
            begin
              frmwait.close;
              break;
            end;
            item_1 := frmmain.lv_info.Items.Add;
            item_1.Caption := trim(rs_1.fields['hlp_code'].value);
            item_1.SubItems.Add(trim(rs_1.fields['hlp_name'].value));
            if (varisnull(rs_1.fields['hlp_exepath'].value)) or (varisempty(rs_1.fields['hlp_exepath'].value)) then
              item_1.SubItems.Add('<无>')
            else
              item_1.SubItems.Add(trim(rs_1.fields['hlp_exepath'].value));
            item_1.SubItems.Add(trim(rs_1.fields['hlp_num'].value));
            if (varisnull(rs_1.fields['note'].value)) or (varisempty(rs_1.fields['note'].value)) then
              item_1.SubItems.Add('<无>')
            else
              item_1.SubItems.Add(trim(rs_1.fields['note'].value));
            rs_1.movenext;
          end;
        end;
        rs_1.close;
      end;  if node.Level = 1 then
      begin
        sql := 'select * from helptype where left(typ_code,2) = (select typ_code from helptype where typ_name = '''+trim(node.Parent.Text)+''' and node = ''1'') and node = ''2'' and typ_name = '''+trim(node.text)+''' ';
        rs_1.open(sql,cnmain,0,1);
        if rs_1.recordcount > 0 then
        begin
           sql := 'select * from helpinfo where left(typ_code,4)= '''+trim(rs_1.fields['typ_code'])+''' order by convert(int,hlp_code)';
           rs_2.open(sql,cnmain,0,1);
           if rs_2.recordcount > 0 then
           begin
             for i := 1 to rs_2.recordcount do
             begin
               if Terminated then
               begin
                 frmwait.close;
                 break;
               end;
               item_1 := frmmain.lv_info.Items.Add;
               item_1.Caption := trim(rs_2.fields['hlp_code'].value);
               item_1.SubItems.Add(trim(rs_2.fields['hlp_name'].value));
               if (varisnull(rs_2.fields['hlp_exepath'].value)) or (varisempty(rs_2.fields['hlp_exepath'].value)) then
                 item_1.SubItems.Add('<无>')
               else
                 item_1.SubItems.Add(trim(rs_2.fields['hlp_exepath'].value));
               item_1.SubItems.Add(trim(rs_2.fields['hlp_num'].value));
               if (varisnull(rs_2.fields['note'].value)) or (varisempty(rs_2.fields['note'].value)) then
                 item_1.SubItems.Add('<无>')
               else
                 item_1.SubItems.Add(trim(rs_1.fields['note'].value));
               rs_2.movenext;
             end;
           end;
        end;
        rs_1.close;
        rs_2.close;
      end;  if node.Level = 2 then
      begin
        sql := 'select * from helptype where left(typ_code,2) = (select typ_code from helptype where typ_name = '''+trim(node.Parent.Parent.Text)+''') and node = ''3'' and typ_name = '''+trim(node.Text)+'''';
        rs_1.open(sql,cnmain,0,1);
        if rs_1.recordcount > 0 then
        begin
          sql := 'select * from helpinfo where left(typ_code,6) = '''+trim(rs_1.fields['typ_code'].value)+''' order by convert(int,hlp_code)';
          rs_2.open(sql,cnmain,0,1);
          if rs_2.recordcount > 0 then
          begin
            for i := 1 to rs_2.recordcount do
            begin
              if Terminated then
              begin
                frmwait.close;
                break;
              end;
              item_1 := frmmain.lv_info.Items.Add;
              item_1.Caption := trim(rs_2.fields['hlp_code'].value);
              item_1.SubItems.Add(trim(rs_2.fields['hlp_name'].value));
              if (varisnull(rs_2.fields['hlp_exepath'].value)) or (varisempty(rs_2.fields['hlp_exepath'].value)) then
                item_1.SubItems.Add('<无>')
              else
                item_1.SubItems.Add(trim(rs_2.fields['hlp_exepath'].value));
              item_1.SubItems.Add(trim(rs_2.fields['hlp_num'].value));
              if (varisnull(rs_2.fields['note'].value)) or (varisempty(rs_2.fields['note'].value)) then
                item_1.SubItems.Add('<无>')
              else
                item_1.SubItems.Add(trim(rs_1.fields['note'].value));
              rs_2.movenext;
            end;
          end;
        end;
        rs_1.close;
        rs_2.close;
      end;
      frmwait.close;
    end;end.
      

  4.   

    啊,你的代码长度跟你的裹脚布差不多吧?
    问题出在哪里?
    装载ListView或者TreeView的时候先把List/Tree|View Disabled了,然后填充,在线程的结束的时候Enable List/Tree|View控件就可以了,如果对于结束或者Borland的包装不是很了解,比如,不知道什么时候结束线程,或是抱怨线程没有按照你的方式工作,那么,用Windows API写,也不是很复杂,你定义一个回调函数,比如:
    function ThreadProc(pData: Pointer): DWORD; stdcall;
    begin
        //handle your trans
    end;
    然后创建线程用CreateThread就可以了,看看MSDN,你会发现很有帮助的,不过,你的代码还是不是很精,你想做什么说说吧,高手们会给你帮助的。
      

  5.   

    怎么Delphi论坛开始有这么多MM了??
    乱拿一条这么长的东西,都不知道你想干什么?
    留下你的QQ吧,我发现我的问题比你还多!!!
      

  6.   

    代码那么长...  线程使用 VCL 要同步
      

  7.   

    wjlsmail(计算机质子)
    是很长
    怎么着!!
    烦着呢!!
      

  8.   

    让我乱写一段吧:...type
      TMyTreeViewUpdateThread = class (TThread)
      private
        m_cTV: TTreeView;
        m_cCS: TCriticalSection;
      protected
        procedure Execute; override;
        procedure OnUpdateTV;
      public
        constructor Create (cTV: TTreeView);
      end;
    implementationprocedure TMyTreeViewUpdateThread.Execute; 
    begin
        // assume m_cTV cleared
      while not Terminated do
      begin
        m_cCS.Enter;
        try
        if StillSomethingTodo then
          DoSomething;
        finally
          m_cCS.Leave;
        end;
        if SomethingDid then
        begin
          Synchronize(OnUpadateTV);
        end;
      end;
    end;procedure TMyTreeViewUpdateThread.OnUpdateTV;
    begin
      m_cCS.Enter;
      try
        if SomethingDid then
        begin
          m_cTV.BeginUpdate;
          m_cTV.AddItem etc....
        end;
      finally
        m_cCS.Leave;
      end;
      if AllDone Terminate;
    end;没认真检查~~~
      

  9.   

    你可以看一下
     delphi安装目录下的一个线程例子,