我下面的代码能实现自动查询了,但是有个很重要的问题,程序很容易出错,动不动就跳出个访问内存错误,
而且在慢慢的消耗我的内存,我看了没什么总题,谁能给解决一下:Procedure TForm1.serch(Const Pages: WideString);
Var
  st:TStringList;
  ASource: PChar;
  TmpStr: string;
  Iscon: Boolean;
Begin
  EndNum:=1;
  errNum:=0;
  st:=Nil;
  Label2.Caption:= inttostr(0);
while (RichEdit1.Lines.Count>0) Do
begin
  If Not IsSt Then break;
  If IE <> Nil Then
    Begin
      If Assigned(IE) Then IE.Free;
      IE := Nil;
    End;
  st:=TStringList.Create;
  RichEdit1.PlainText := true;
  Iscon:= false;
  if errNum=8 then
    begin
    RichEdit1.Lines.Insert(0,RichEdit4.Lines[0]);
    errNum:=0;
    Label4.Caption:= IntToStr(0);
    end;
  ASource:= PChar(RichEdit1.Lines[0]);
try
  ExtractStrings([':'],[],ASource,st);
if st.Count>4 then
begin
  IE := TWebBrowser.Create(int_title); //使用动态创建此控件,可有效控制内存占用问题
  IE.parentWindow := int_title.Handle;
  IE.Silent := true;
  IE.Height := 500;
  IE.Width := 587;
  IE.Left := -70;
  IE.Top := -200;
  IE.OnNewWindow2 := IENewWindow2;
  IE.OnStatusTextChange := IEStatusTextChange;
  IE.OnTitleChange := SetTitleText;
  Wait(300);
  IE.Navigate(Pages);
  While IE.ReadyState <> READYSTATE_COMPLETE Do application.ProcessMessages;  if (Pos(LowerCase('没有可以显示的页面'), LowerCase(int_title.Caption)) > 0) or (Pos(LowerCase('errorCode'), LowerCase(IE.LocationURL)) > 0)Then
    begin
    SB.Panels[1].Text :=  'Pless Change Proxy!!';
    Windows.Beep(660, 100);
    break;
    end;
  if Not IsSt then  break;
  If (LowerCase(IE.LocationURL) = LowerCase(Pages1)) Then
    WhiteData(IE, true,st);
  while (Not IsCustomPass) and (Pos(LowerCase('login'), LowerCase(IE.LocationURL)) > 0) do
  begin
    TmpStr := (IE.Document As IHTMLDocument2).body.outerText;
    If (Pos('登陆失败', TmpStr) > 0) Then
    begin
              RichEdit3.Lines.Add(st.CommaText+#9+'P');
              RichEdit1.Lines.Delete(0);
              Iscon:=true;
              errNum:=errNum+1;
              Label4.Caption:= IntToStr(errNum);
              break;
    end else
    Wait(3000);
  end;
  if Iscon then Continue;
  If IsSt and (Pos(LowerCase('services_error'), LowerCase(IE.LocationURL)) > 0) Then
  begin
    RichEdit3.Lines.Add(st.CommaText+#9+'YF');
    RichEdit1.Lines.Delete(0);
    Continue;
  end;
  While IE.ReadyState <> READYSTATE_COMPLETE Do application.ProcessMessages;
  if Not IsSt then  break;
  If IsSt and (Pos(LowerCase('shop_list.aspx'), LowerCase(IE.LocationURL)) > 0) Then
  begin
    errNum:=0;
    Label4.Caption:= IntToStr(errNum);
    TmpStr := (IE.Document As IHTMLDocument2).body.outerText;
    If (Pos('我的商店', TmpStr) > 0) Then
    begin
              RichEdit3.Lines.Add(st.CommaText+#9+'K');
              RichEdit1.Lines.Delete(0);
              Continue;
    end else
    begin
              RichEdit2.Lines.Add(st.CommaText+#9+'J');
              RichEdit1.Lines.Delete(0);
              Label2.Caption:= IntToStr(EndNum);
              EndNum:=EndNum+1;
    end;
  end
  else if IsSt and (Pos(LowerCase('要激活您的商店'), LowerCase(IE.LocationURL)) > 0) Then
  begin
    errNum:=0;
    Label4.Caption:= IntToStr(errNum);
    RichEdit2.Lines.Add(st.CommaText+#9+'NJ');
    RichEdit1.Lines.Delete(0);
    Label2.Caption:= IntToStr(EndNum);
    EndNum:=EndNum+1;
  end
  else if IsSt and (Pos(LowerCase('禁用的商店'), LowerCase(IE.LocationURL)) > 0) Then
  begin
    errNum:=0;
    Label4.Caption:= IntToStr(errNum);
    RichEdit3.Lines.Add(st.CommaText+#9+'F');
    RichEdit1.Lines.Delete(0);
    Continue;
  end  else
  begin
    break;
  end;
end else
break
finally
  st.Free;
end;
end;
If IE <> Nil Then
Begin
  If Assigned(IE) Then IE.Free;
  IE := Nil;
End;
    IsSt := false;
    Button1.Caption := 'Start';
End;
上面的代码还有个问题我没有找到解决办法如下面的代码
  while (Not IsCustomPass) and (Pos(LowerCase('login'), LowerCase(IE.LocationURL)) > 0) do
  begin
    TmpStr := (IE.Document As IHTMLDocument2).body.outerText;
    If (Pos('登陆失败', TmpStr) > 0) Then
    begin
              RichEdit3.Lines.Add(st.CommaText+#9+'P');
              RichEdit1.Lines.Delete(0);
              Iscon:=true;
              errNum:=errNum+1;
              Label4.Caption:= IntToStr(errNum);
              break;
    end else
    Wait(3000);
  end;
这段代码是当网页没有正常进入到下一个页面时就开始循环等,但是有时网速慢或是网页打不开这样就会死在这里了.
还有我想暂停的时候要等IE执行完才能停下来很麻烦,所以我做了个按钮强行IE.free, 这时又会跳出内存错误,刚开始学这东西,不太明白,请专家指点一二

解决方案 »

  1.   

    等待的意思吧,,,sleep去感觉动态生成的控件容易出错的,我一个多窗口程序,除了主界面外别的都采用动态生成。。Y的,好难,后来干脆全用静态的窗体了。。
      

  2.   

    你看你哪一句代码访问内存出错的
    一般都是对象没创建要么对象已经释放还使用
    If IE <> Nil Then
        Begin
          If Assigned(IE) Then IE.Free;
          IE := Nil;
        End;
    这个最好用FreeAndNil 如果IE只是free没有nil的话就要出错了!
      

  3.   

    判断是否下载完:
    IE.DefaultInterface.ReadyState = READYSTATE_COMPLETE
      

  4.   

    如果没有下载完就访问IE.Document 就有可能会报异常。