有关thread的问题?(30分)

S

sugzh

Unregistered / Unconfirmed
GUEST, unregistred user!
我写了一个有关THREAD的小程序,编译通过,但一运行就出错。
现将程序贴在此,请诸位大虾,指点小弟一二,小弟万万分感激!
小弟的信箱:su@263.net
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;

type
thread = class(tthread)
private
fpanel : tpanel;
protected
procedure draw;
procedure execute
override;
public
constructor create(panel:tpanel);
end;

TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Image1: TImage;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
test : thread;
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
constructor thread.create(panel:tpanel);
begin
inherited create(false);
fpanel:=panel;
freeonterminate:=true;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
form1.left:=0;
form1.width:=800;
panel1.left:=0;
panel1.width:=800;
test.create(panel1);
end;

procedure thread.draw;
var
delay :tdatetime;
hour,min,sec,msec,temp:word;
begin
delay:=time;
decodetime(delay,hour,min,sec,msec);
temp:=msec+100;
while temp>msec do
begin
delay:=time;
decodetime(delay,hour,min,sec,msec);
end;
fpanel.left:=fpanel.left+1;
end;

procedure thread.execute
var
i : integer;
begin
i:=0;
while i<10 do
begin
synchronize(draw);
i:=5;
end;
end;
end.
 
在 Thread 中不能直接用 VCL component, 要用 Synchronize 方法同步.
详见 TThread 的帮助.

fpanel.left:=fpanel.left+1;
^^^^^^^^^^^^^^^^^^^^^^^^^^^

 
呵呵, 尽管您的程序没有贴全, 我还是根据我的猜测发现了若干问题:
1.
test.create(...) should be:
test := thread.Create(...);

2,
如果您是想让Form中的Panel动起来, 那么您必须传递
Panel的指针, 进行如下修改:
a) uint, interface part:
type
TPPanel = ^ TPanel;

b) thread object type part:
constructor create(PPanel: TPPanel);

c) member of thread object:
pfpanel: TPPanel;

d) TForm1.Create part:
test := thread.Create(@Panel1);

e) thread.create part:
inherit create(TRUE)
{ suspend after create so that I can init variable s}
pfpanel := ppanel;
freeonterminate := true;
resume;

Now this program runs smoothly here, let the panel move from left side to right side.

Hehe, good luck!
(But the score is so little, :(, :)
 
In this program, without synchronization is also OK.
 
谢谢,pegasus大虾的指点。但我还想问你怎样做才能使一个控件在屏幕上移动时
不产生抖动感。还有在/demos/threads中的例子中它用的是
with TBubbleSort.Create(BubbleSortBox, BubbleSortArray) do
(注: TBubbleSort = class(TSortThread)
protected
procedure Sort(var A: array of Integer)
override;
end;
BubbleSortBox: TPaintBox;)
能告诉我,这两种用法的区别吗?
再次谢谢!
 
>能告诉我,这两种用法的区别吗?
嚯, Borland的例子够精干的, 连创建出的对象都懒得写了,
它应当等价于:
Var
bubbleSortThread : TBubbleSort;
...

bubbleSortThread := TBubbleSort.Create(BubbleSortBox, BubbleSortArray);
with bubbleSortThread do

例子程序中的写法并不好!

另外: 我前面所说得不太对, 不需要传递 TPPanel 这样的类型,
您原先的这部分不用修改也可以使Panel动起来
Delphi把对象变量自动当作指针来赋值的. 我对她这一点很恼火!
分明是混淆概念!

要想让大的控件在移动时不产生抖动感, 恐怕您必须直接用
WindowsAPI 的SetWindowPos来移动她们. 效果也不一定好,
除非您用双缓冲技术, 这样似乎太不值得了?
 
说到这里,小弟更要向pegasus大虾请教了。
小弟对于WINDOWS 中的消息,感到无从下手。
在处理某一个问题时,感到一个消息,却不知
道怎样到WINDOWS中去找。同时也希望pegasus
大虾能告诉我一些常用的WINDOWS API。
小弟十分惭愧,仅仅30分。却问了怎么多问题。
请pegasus大虾间量。小弟实在是个新手。
谢谢!
 
Xixi, 谁叫咱是摊主呢, 就不能计较分数啦!
不知道你有没有Visual C++的 帮助文件, 那里面可以用模糊查找
技术, 比如您想在Windows的位置移动时接到通知, 可以用
"Position changed"去找. 不过有时候还是必须知道一些专用名词
及其缩写才能够容易找到需要了解的消息, 例如WM_NCMOUSEMOVE
您必须知道NonClient area这样的说法

至于常用的Windows API, 分为进程管理, 文件管理, 内存管理,
窗口管理, GDI绘图函数, 注册表管理, 系统级别的特殊应用等
如果您没有Visual C++, 那么可能还是找本书看看好.
 
接受答案了.
 

Similar threads

I
回复
0
查看
568
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
604
import
I
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
顶部