多线程应该用(blog更新频道)

                            unit uThreadUpdate;
{-------------------------------------------------------------
说明:自动更新RSS xml文件线程
功能: 无
作者:lihuizhan
--------------------------------------------------------------}

interface

uses
  Classes,SysUtils,Dialogs,RSSGroup,RSSChannel,Windows;

type
  coThreadUpdate = class(TThread)
  private
    { Private declarations }
    FchannelTitle:string;//
    FChannelIndex:integer;//频道索引,更新完成后通过消息发送回主线程更新界面
  protected
    procedure Execute; override;
  public
    Constructor Create(ChannelTitle:string;ChannelIndex:integer);{传入channel的title创建新的线程}
    destructor Destroy;override;
    procedure UpdateChannel(rssGroup :TRSSGroup;caption: string;Aower:TComponent);



  end;

implementation
uses uMain,uSystemVar,ActiveX,RSSGroupItem,DataAdapter,RSSNewsItem;
{ 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 coThreadUpdate.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }

{ coThreadUpdate }
{ChannelTitle: string=频道的标题}
{ChannelIndex:integer=频道在频道组中的索引位置,用作发送消息更新指定频道的Grid的数据}
constructor coThreadUpdate.Create(ChannelTitle: string;ChannelIndex:integer);
begin
    FchannelTitle:=ChannelTitle;
    FChannelIndex:=ChannelIndex;
    inherited Create(false);//启动
end;

destructor coThreadUpdate.Destroy;
begin
  FchannelTitle:='';
  inherited;
end;



{更新频道组的内容到本地XML文件}
procedure coThreadUpdate.UpdateChannel(rssGroup :TRSSGroup;caption: string;Aower:TComponent);
var
    item                :TRSSGroupItem;//
    strList : TList ;
    newsItem            :TRSSNewsItem ;
    New_Channel : TRSSChannel ;//新旧两个channel xml文件
    Old_Channel : TRSSChannel ;
    i     :integer;
begin

    New_Channel := TRSSChannel.create ;
    //
    item:=FindGroupItem(caption);  //CSDN技术文档中心-Delphi'

    //先从本地取得旧的channnel XML文件
    Old_Channel:= FindLoacalChannelFile(item.getTitle,Aower);
    if Old_Channel<> nil then
    begin
        try
            //从internet上最得最新xml
            New_Channel:=rssGroup.getChannel( item.getXmlUrl, false , Aower ) ;
            //监测channel
            rssGroup.watchChannel(DataFilePath,New_Channel,Aower);
            //合并到本地
            Old_Channel.appendItems(ExeRunPath+'data\',New_Channel);
            //更新完毕发送消息告诉主线程
            sendmessage(Mainfrm.Handle,MESSAGE_THREAD,0,FChannelIndex);   //
        except
            MessageBox(Mainfrm.handle,pchar(item.getXmlUrl+'上的xml文件可能有误,请检查其详细内容是否合符RSS/RDF标准!'),pchar('错误提示'),0);//不能用showmessage,因为它是不是线程安全的
        end;
    end
    else MessageBox(Mainfrm.handle,pchar('Channels文件夹中不存在文件名为"'+item.getTitle+'"的xml文件'),pchar('文件不存在'),0);//不能用showmessage,因为它是不是线程安全的
end;

procedure coThreadUpdate.Execute;
begin
    FreeOnTerminate:=true;
    CoInitialize(nil);
        //  Synchronize( DoUpdateChannel);

        UpdateChannel(GroupList[Mainfrm.imgcobGroups.ItemIndex],FchannelTitle,Mainfrm);
    CoUninitialize;
  { Place thread code here }
end;


end.
                                

查看回复