小弟我刚学多线程,自己做了一个程序,可是总是出错,代码如下:(10分)

  • 主题发起人 主题发起人 huanzhugege
  • 开始时间 开始时间
H

huanzhugege

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟我刚学多线程,自己做了一个程序,可是总是出错,代码如下:
主程序:
crazyclick1:Tcrazyclick;//tcrazyclick是我自定义的一个线程
...
procedure TForm1.Button1Click(Sender: TObject);
begin
crazyclick1.Resume;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
crazyclick1:=Tcrazyclick.create(edit1.Text);
end;

Tcrayclick线程代码
unit crazyclick;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, ExtCtrls;
type
Tcrazyclick = class(TThread)
webbrowser1:Twebbrowser;
private
{ Private declarations }
protected
url1:string;
procedure Execute;
override;
procedure webbrowser1Downloadcomplete(sender:Tobject);
public
finishednum:integer;
constructor create(url:string);
end;

implementation
constructor Tcrazyclick.create(url:string);
begin
inherited create(True);
finishednum:=0;
url1:=url;
end;

procedure Tcrazyclick.webbrowser1Downloadcomplete(sender:Tobject);
begin
try
webbrowser1.Refresh;
inc(finishednum);
except
on exceptiondo
end;

end;

procedure Tcrazyclick.Execute;
begin

webbrowser1.Navigate(url1);//执行到此处时总是出错
{ Place thread code here }
end;

end.
执行到上边注释的那句时总是出错,请帮助我
 
procedure TForm1.FormCreate(Sender: TObject);
begin
crazyclick1:=Tcrazyclick.create(edit1.Text);///////////////////////////
end;
///////////////////////
那句错了,应为crazyclick1:=Tcrazyclick.create(false);
 
crazyclick1:=Tcrazyclick.create(edit1.Text)[?]
没见过????????????
应该是一个布尔型的吧一般用true or false
 
重写一下create接受你的edit
 
procedure Tcrazyclick.Execute;
begin
webbrowser1.Navigate(url1);//访问主线程中的vcl控件要用Synchronize方法同步
end;
 
当然会出错了,你访问的是主窗口的可视化组件而且没有使用同步,很容易发生冲突的!改一下:
crazyclick1:Tcrazyclick;//tcrazyclick是我自定义的一个线程
...
procedure TForm1.Button1Click(Sender: TObject);
begin
crazyclick1.Resume;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
crazyclick1:=Tcrazyclick.create(edit1.Text,webbrowser1);
end;

Tcrayclick线程代码
unit crazyclick;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, StdCtrls, ExtCtrls;
type
Tcrazyclick = class(TThread)
private
webbrowser:Twebbrowser;
{ Private declarations }
protected
url1:string;
procedure Execute;
override;
procedure webbrowser1Downloadcomplete(sender:Tobject);
procedure exec;
procedure execd;
public
finishednum:integer;
constructor create(url:string;webbrowserq:Twebbrowser);
end;

implementation
constructor Tcrazyclick.create(url:string;webbrowserq:Twebbrowser);
begin
inherited create(True);
finishednum:=0;
url1:=url;
webbrowser:=webbrowserq;
end;

procedure Tcrazyclick.execd;
begin
try
webbrowser.Refresh;
inc(finishednum);
except
on exceptiondo
raise;
end;
end;

procedure Tcrazyclick.webbrowser1Downloadcomplete(sender:Tobject);
begin
Synchronize(execd);
end;

procedure Tcrazyclick.exec;
begin
webbrowser.Navigate(url1);
end;

procedure Tcrazyclick.Execute;
begin
Synchronize(exec);
{ Place thread code here }
end;
end.
将所有涉及可视化的方面都当主线程去做,工作线程发送消息就可以了,没有在机子上测试这段代码,你自己试试吧!
 
同意 晶晶 同志的观点。
 
开始学习线程的时候,建议先看点资料Delphi就自带一个Demo,另外,下载一个Delphibbs(帮助文件格式)来看看。
 
后退
顶部