delphi做的小程序长时间运行,就无响应了!(100分)

  • 主题发起人 主题发起人 meme258
  • 开始时间 开始时间
M

meme258

Unregistered / Unconfirmed
GUEST, unregistred user!
delphi做的小程序长时间运行,就无响应了!<br>主要就是一个Webbrowser 不停的刷新3个网页,有的时候几个小时就无响应了,有的时候几天没响应。<br>新手求救。。。。 。。。<br><br>unit Unit1;<br><br>interface<br><br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls, OleCtrls, SHDocVw, ComCtrls,inifiles,shellapi, ExtCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; WebBrowser1: TWebBrowser;<br>&nbsp; &nbsp; GroupBox1: TGroupBox;<br>&nbsp; &nbsp; GroupBox2: TGroupBox;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; Edit3: TEdit;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; StatusBar1: TStatusBar;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Edit4: TEdit;<br>&nbsp; &nbsp; Button4: TButton;<br>&nbsp; &nbsp; Timer1: TTimer;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Label3: TLabel;<br>&nbsp; &nbsp; Button5: TButton;<br>&nbsp; &nbsp; Button6: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Timer1Timer(Sender: TObject);<br>&nbsp; &nbsp; procedure Button5Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button6Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure FormCreate(Sender: TObject);<br>&nbsp; &nbsp; procedure FormDestroy(Sender: TObject);<br>&nbsp; &nbsp; function CheckThreadFreed(aThread: TThread): Byte;<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br>&nbsp; i,zti:integer;<br><br>implementation<br><br>uses Unit2, Unit3;<br>var<br>&nbsp; myinifile: TInifile;<br>&nbsp; //timThread:timeThread;<br>&nbsp; //id1,id2,id3:string;<br>{$R *.dfm}<br><br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; tOne : cThread;<br>begin<br>&nbsp; zti := 1;<br>&nbsp; tOne := cThread.Create(false);<br>&nbsp; //WebBrowser1.Navigate(edit1.Text);<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br>&nbsp; timer1.Enabled :=true;<br>&nbsp; label2.Caption := '开始';<br><br>end;<br><br>procedure TForm1.Timer1Timer(Sender: TObject);<br>begin<br>&nbsp; application.HandleMessage;<br>&nbsp; //timThread := timeThread.Create(false);<br>&nbsp; &nbsp;if &nbsp;webbrowser1.LocationURL = '' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; timer1.Enabled := false;<br>&nbsp; &nbsp; label2.Caption := '采集未运行' ;<br>&nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; if pos('collectpagenum=' + edit4.Text ,webbrowser1.LocationURL) &nbsp;&lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; webbrowser1.Navigate(edit1.text);<br>&nbsp; end;<br><br>&nbsp; label3.Caption := IntToStr(i) ;<br>&nbsp; i := i+1;<br>&nbsp; &nbsp;statusbar1.Panels.Items[1].Text := webbrowser1.LocationURL;<br>&nbsp; if webbrowser1.Busy = false then<br>&nbsp; &nbsp; webbrowser1.Refresh;<br><br>&nbsp; &nbsp; //showmessage('s');<br>&nbsp; &nbsp;// showmessage(webbrowser1.LocationURL);<br>&nbsp; <br>end;<br><br>procedure TForm1.Button5Click(Sender: TObject);<br>begin<br>&nbsp; timer1.Enabled := false;<br>&nbsp; label2.Caption := '停止' ;<br>end;<br><br>procedure TForm1.Button6Click(Sender: TObject);<br>begin<br>&nbsp; webbrowser1.Stop;<br>&nbsp; timer1.Enabled := false;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var<br>&nbsp; tTwo : cThread;<br>begin<br>&nbsp; zti := 2;<br>&nbsp; tTwo := cThread.Create(false);<br>&nbsp; //WebBrowser1.Navigate(edit2.Text);<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br>&nbsp; tThere : cThread;<br>begin<br>&nbsp; zti := 3;<br>&nbsp; tThere := cThread.Create(false);<br>&nbsp; //WebBrowser1.Navigate(edit3.Text);<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; filename: string;<br>begin<br>&nbsp; filename := ExtractFilePath(paramstr(0)) + '1.ini';<br>&nbsp; MyINIFile := TINIFile.Create(filename);<br>&nbsp; edit1.Text := myinifile.readstring('url', 'url1', '');<br>&nbsp; edit2.Text := myinifile.readstring('url', 'url2', '');<br>&nbsp; edit3.Text := myinifile.readstring('url', 'url3', '');<br>&nbsp; button1.Caption := &nbsp;myinifile.readstring('an', 'an1', '');<br>&nbsp; button2.Caption := &nbsp;myinifile.readstring('an', 'an2', '');<br>&nbsp; button3.Caption := &nbsp;myinifile.readstring('an', 'an3', '');<br>&nbsp; MyINIFile.Free;<br>&nbsp;// id1:= myinifile.readstring('id', 'id1', '');<br>&nbsp; //id2:= myinifile.readstring('id', 'id2', '');<br>&nbsp;// id3:= myinifile.readstring('id', 'id3', '');<br>end;<br><br>procedure TForm1.FormDestroy(Sender: TObject);<br>begin<br><br>&nbsp; &nbsp;//shellexecute(handle,'open',pchar('http://www.tomzw.com'),nil,nil,sw_show);<br>end;<br><br><br>function TForm1.CheckThreadFreed(aThread: TThread): Byte;<br>var <br>&nbsp;i: DWord; <br>&nbsp;IsQuit: Boolean; <br>begin <br>&nbsp;if Assigned(aThread) then <br>&nbsp;begin <br>&nbsp; &nbsp;IsQuit := GetExitCodeThread(aThread.Handle, i); <br>&nbsp; &nbsp;if IsQuit then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //If the function succeeds, the return value is nonzero. <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//If the function fails, the return value is zero. <br>&nbsp; &nbsp;begin <br>&nbsp; &nbsp; &nbsp;if i = STILL_ACTIVE then &nbsp; &nbsp;//If the specified thread has not terminated, <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//the termination status returned is STILL_ACTIVE. <br>&nbsp; &nbsp; &nbsp; &nbsp;Result := 1 <br>&nbsp; &nbsp; &nbsp;else <br>&nbsp; &nbsp; &nbsp; &nbsp;Result := 2; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//aThread未Free,因为Tthread.Destroy中有执行语句 <br>&nbsp; &nbsp;end <br>&nbsp; &nbsp;else <br>&nbsp; &nbsp; &nbsp;Result := 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//可以用GetLastError取得错误代码 <br>&nbsp;end <br>&nbsp;else <br>&nbsp; &nbsp;Result := 3; <br>end;<br><br>end.<br>===============================================================================<br><br>unit Unit2;<br><br>interface<br><br>uses<br>&nbsp; Classes;<br><br>type<br>&nbsp; cThread = class(TThread)<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Execute; override;<br>&nbsp; &nbsp; procedure UpdateCaption;<br>&nbsp; end;<br><br>implementation<br><br>uses Unit1;<br><br>{ Important: Methods and properties of objects in visual components can only be<br>&nbsp; used in a method called using Synchronize, for example,<br><br>&nbsp; &nbsp; &nbsp; Synchronize(UpdateCaption);<br><br>&nbsp; and UpdateCaption could look like,<br><br>&nbsp; &nbsp; procedure cThread.UpdateCaption;<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; Form1.Caption := 'Updated in a thread';<br>&nbsp; &nbsp; end; }<br><br>{ cThread }<br><br>procedure cThread.Execute;<br>begin<br>&nbsp; { Place thread code here }<br>&nbsp; FreeOnTerminate := True;<br>&nbsp; Synchronize(UpdateCaption);<br>&nbsp; form1.Caption := '采集线程在运行';<br>end;<br><br>procedure cThread.UpdateCaption;<br>begin<br>&nbsp; case zti of<br>&nbsp; &nbsp; 1:form1.WebBrowser1.Navigate(form1.Edit1.Text);<br>&nbsp; &nbsp; 2:form1.WebBrowser1.Navigate(form1.Edit2.Text);<br>&nbsp; &nbsp; 3:form1.WebBrowser1.Navigate(form1.Edit3.Text);<br>&nbsp; end; &nbsp;<br>end;<br><br>end.
 
线程里面不要用控件
 
线程里不要使用TControl方面的控件,如果要使用,你必须使用Thread.同步<br>比如:<br>procedure Tthread.addlog(str:string)<br>begin<br>self.tempstr=str;<br>self.Synchronize(Log);<br>//因为Synchronize不支持参数形式的...如果要支持,这种方式是最简单的,也可以重写 根据Synchronize的写法,自己写一个<br>end;<br><br>procedure tthread.log;<br>begin<br>//在Synchronize中运行,因为在和其它控件通讯时,要用线程同步方式,<br>application也是一个线程,和你的线程间要同步处理(锁定,运行用户代码,再解锁)<br>form1.caption:=str;<br>end;<br><br><br>还有一个问题,我看你上面用了Webbrowser,我没有仔细看,但是Webbrowser是有内存泻漏的,<br>就算你定时的free,也没用,运行久了,是会有这种问题
 
谢谢 3304xfq<br>去试试看先!!!
 
多人接受答案了。
 
这个问题终于解决了,<br><br>原来是 webbrowser 的问题,有人说 webbrowser 会内存泄露,时间长了以后就会出现这种情况,程序里面用 embeddedwb 替换webbrowser 以后这个问题解决了,<br><br>感谢各位的支持,回答,谢谢
 
后退
顶部