执行老有错误,请指导啊。只有100分,全部奉上!
*********************************************************************
*********************************************************************
unit self_tthread_weber;interfaceuses
  Classes
  , ShDocVw //webbrowser
  ,MSHTML     //IHtmlDocument2
  ,SysUtils   //strtoint
  ,prog      //全局变量
  ,self_forum  //login type
  ,self_tthread_check    //检测
  ;
type
  tthread_weber = class(TThread)
  private
    { Private declarations }
    owner:tcomponent;
    weber:twebbrowser;
    urlstr:string;
    Fthreadcheck:tthread_check;
  protected
    procedure Execute; override;
  public
    constructor Create(aowner:tcomponent;url:string;suspend:boolean);
    //distructor
    procedure weberdocComplete(Sender: TObject;const pDisp: IDispatch; var URL: OleVariant);
  end;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 tthread_weber.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ tthread_weber }procedure tthread_weber.Execute;
begin
  { Place thread code here }
  Fthreadcheck.Resume;
  Fthreadcheck.WaitFor;
end;/////public
constructor tthread_weber.Create(aowner:tcomponent;url:string;suspend:boolean);
begin
    owner:=aowner;
    urlstr:=url;
    weber:=twebbrowser.Create(aowner);
    //aowner.InsertControl(weber);
    weber.Navigate(url);
    Fthreadcheck:=tthread_check.create(weber.Tag,2002,8000,true);
    weber.OnDocumentComplete:=weberdoccomplete;
end;
////////////////////////////////////
procedure tthread_weber.weberdocComplete(Sender: TObject;const pDisp: IDispatch; var URL: OleVariant);
{
var
    listtmp:tstrings;
    lineindex:integer;
    }
begin
    {
    listtmp:=tstringlist.create;
    listtmp.Add(IHtmlDocument2(Weber.Document).Body.OuterText);
    listtmp.SaveToFile(exeinfo.tempfile);
    listtmp.loadfromFile(exeinfo.tempfile);
    if listtmp.Strings[0]='y' then
      begin
       dz.setlogined(loginyes);
       dz.setusername(listtmp.Strings[1]);
       dz.setuid(strtoint(trim(listtmp.Strings[2])));
      end
    else
      begin
       dz.setlogined(loginno);
      end;
    deletefile(pchar(exeinfo.tempfile));
    listtmp.free;
    }
    weber.Tag:=2002;
end;
end.
*********************************************************************
*********************************************************************
unit self_tthread_check;
interfaceuses
  Classes
  , ShDocVw //webbrowser
  ,MSHTML     //IHtmlDocument2
  ,SysUtils   //strtoint
  ,prog  //全局变量
  ,self_forum  //set login
  ,Dialogs   //showmessage;
  ;type
  tthread_check = class(TThread)
  private
    { Private declarations }
    ex:integer;
    value:integer;
    timeout:integer;
  protected
    procedure Execute; override;
  public
  constructor Create(express:integer;pvalue:integer;ptimeout:integer;suspend:boolean);
  end;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 tthreadweber.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end; }{ tthreadweber }procedure tthread_check.Execute;
var
  times:integer;
begin
  { Place thread code here }
  //showmessage('thread start');
  times:=0;
  self.ReturnValue:=1;
  while(ex<>value) and (times<timeout) do
  begin
     sleep(1000);
     times:=times+1000;
  end;
end;
constructor tthread_check.Create(express:integer;pvalue:integer;ptimeout:integer;suspend:boolean);
begin
     inherited Create(true);
     EX:=EXPRESS;
     value:=pvalue;
     FreeOnTerminate:=true;
     timeout:=ptimeout;
     if not suspend then resume; end;
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
end.

解决方案 »

  1.   

    不能直接跨线程访问MSHTML
      

  2.   

    谢谢指导,但是不明白你的意思,访问webbrowser的内容是在同一个线程里面的呀。跨访了webbrowser.tag 这个可以吗?
    修改fthread_check.FreeOnTerminate:=true;之后没有报错了,但是weberdocComplete始终不被执行,(webbrowser的ondocumentcomplete事件处理)
    /////////////////////////////////////////////////////////////
    //修改后的程序:
    ////////////////////////////////////////////////////////////
    unit self_tthread_weber;interfaceuses
      Classes
      , ShDocVw //webbrowser
      ,MSHTML     //IHtmlDocument2
      ,SysUtils   //strtoint
      ,prog      //全局变量
      ,self_forum  //login type
      ,self_tthread_check    //检测
      ,dialogs
      ;
    type
      tthread_weber = class(TThread)
      private
        { Private declarations }
        owner:tcomponent;
        weber:twebbrowser;
        urlstr:string;
        Fthreadcheck:tthread_check;
      protected
        procedure Execute; override;
      public
        constructor Create(aowner:tcomponent;url:string;suspend:boolean);
        //distructor
        procedure weberdocComplete(Sender: TObject;const pDisp: IDispatch; var URL: OleVariant);
      end;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 tthread_weber.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ tthread_weber }procedure tthread_weber.Execute;
    begin
      { Place thread code here }
      Fthreadcheck.Resume;
      Fthreadcheck.WaitFor;
      Fthreadcheck.Free;
     // self.DoTerminate;
    end;/////public
    constructor tthread_weber.Create(aowner:tcomponent;url:string;suspend:boolean);
    begin
        inherited Create(true);
        owner:=aowner;
        urlstr:=url;
        weber:=twebbrowser.Create(aowner);
        //aowner.InsertControl(weber);
        weber.Navigate(url);
        Fthreadcheck:=tthread_check.create(weber.Tag,2002,8000,true);
        weber.OnDocumentComplete:=weberdoccomplete;
        if not suspend then resume; 
    end;
    ////////////////////////////////////
    procedure tthread_weber.weberdocComplete(Sender: TObject;const pDisp: IDispatch; var URL: OleVariant);
    var
        listtmp:tstrings;
        lineindex:integer;begin
        showmessage('in weberdoccomplte!');
        listtmp:=tstringlist.create;
        listtmp.Add(IHtmlDocument2(Weber.Document).Body.OuterText);
        listtmp.SaveToFile(exeinfo.tempfile);
        listtmp.loadfromFile(exeinfo.tempfile);
        if listtmp.Strings[0]='y' then
          begin
           dz.setlogined(loginyes);
           dz.setusername(listtmp.Strings[1]);
           dz.setuid(strtoint(trim(listtmp.Strings[2])));
          end
        else
          begin
           dz.setlogined(loginno);
          end;
        deletefile(pchar(exeinfo.tempfile));
        listtmp.free;    weber.Tag:=2002;
    end;
    end.
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    unit self_tthread_check;
    interfaceuses
      Classes
      , ShDocVw //webbrowser
      ,MSHTML     //IHtmlDocument2
      ,SysUtils   //strtoint
      ,prog  //全局变量
      ,self_forum  //set login
      ,Dialogs   //showmessage;
      ;type
      tthread_check = class(TThread)
      private
        { Private declarations }
        ex:integer;
        value:integer;
        timeout:integer;
      protected
        procedure Execute; override;
      public
      constructor Create(express:integer;pvalue:integer;ptimeout:integer;suspend:boolean);
      end;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 tthreadweber.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ tthreadweber }procedure tthread_check.Execute;
    var
      times:integer;
    begin
      { Place thread code here }
      //showmessage('thread start');
      times:=0;
      self.ReturnValue:=1;
      while(ex<>value) and (times<timeout) do
      begin
         sleep(1000);
         times:=times+1000;
      end;
      self.FreeOnTerminate:=false;
    end;
    constructor tthread_check.Create(express:integer;pvalue:integer;ptimeout:integer;suspend:boolean);
    begin
         inherited Create(true);
         EX:=EXPRESS;
         value:=pvalue;
         FreeOnTerminate:=true;
         timeout:=ptimeout;
         if not suspend then resume; end;end.
      

  3.   

    新问题,一样给分run 后 5秒,弹出cpu窗口如何解决呀?很是郁闷,再按3次run后正常。每次都这样,连新建一个空的也这样。