帮个忙:多线程问题。(100分)

  • 主题发起人 主题发起人 yinhes
  • 开始时间 开始时间
Y

yinhes

Unregistered / Unconfirmed
GUEST, unregistred user!
我的程序源程:
unit multiThread;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
type
TexThread1 = class(TThread)
private
{ Private declarations }
fImage:TImage;
protected
procedure Execute;
override;
public
constructor Create(Image:TImage);
end;
//******Thread2*********************//
type
TexThread2 = class(TThread)
private
{ Private declarations }
fImage:TImage;
protected
procedure Execute;
override;
public
constructor Create(Image:TImage);
end;
var
Form1: TForm1;
Thread1:TexThread1;
Thread2:TexThread2;
csImage: TRTLCriticalSection;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
begin
InitializeCriticalSection(csImage);
Thread1:=TexThread1.Create(Image1);
Thread2:=TexThread2.Create(Image1);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Thread2.Resume;
Thread1.Resume;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Thread2.Suspend;
Thread2.Suspend;
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
DeleteCriticalSection(csImage);
end;



//implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TexThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TexThread }
procedure TextOutImage(Image:TImage;Num,x,y:Integer);
begin
EnterCriticalSection(csImage);
Form1.Image1.Canvas.TextOut(x,y,IntToStr(Num));
Form1.Image1.Refresh;
LeaveCriticalSection(csImage);
end;
constructor TexThread1.Create(Image:TImage);
begin
fImage:=Image;
inherited Create(True);
end;
procedure TexThread1.Execute;
var
i,j:Integer;
iNum:Integer;
begin
{ Place thread code here }
for i:=1 to 10do
for j:=1 to 10do
begin
iNum:=Random(100)*100;
TextOutImage(fImage,iNum,10,20);
Application.ProcessMessages;
Sleep(0)
end;

end;
constructor TexThread2.Create(Image:TImage);
begin
fImage:=Image;
inherited Create(True);
end;
procedure TexThread2.Execute;
var
i,j:Integer;
iNum:Integer;
begin
{ Place thread code here }
for i:=1 to 10do
for j:=1 to 10do
begin
iNum:=Random(100)*200;
TextOutImage(fImage,iNum,10,80);
Application.ProcessMessages;
sleep(0)
end;

end;
end.
问题是:
1. 最小化再最大化后,Image别不能显示。
2. 我去掉csImage子后,Timage仍不冲突。
3. 去掉sleep(0)函数后,总是这显示一个值。而起总是先 resume的进程。
 
1. 要将TextOut 防到paint中去。
3. Sleep(0),是将CPU交给同等优先急的线程.
 
将TextOut放到paint中去是什么意思呢?请指点呀。
 
线程运行时不一定冲突,而是可能会冲突,对VCL资源操作
尽量放到Sychronize中去,让Delphi自己控制,
sleep(0)只是你的这个线程sleep(0)后才执行下一条,
不干涉别的线程
 
使用Sychronize的过程不带参数,用此来不太方便的我想用临界区去
保护效果一样吧?(不知道),还有如果在execute中有几个对同一个vcl
的操作用临界区好象也不太对?
 
to yinhes:
老大,我把你的source copy到我的d4上,very正常啊!
 
DELPHI 中提供了进行资源锁定的功能.你可以在HELP中查找LOCK关键字.
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
709
import
I
I
回复
0
查看
778
import
I
I
回复
0
查看
763
import
I
I
回复
0
查看
566
import
I
I
回复
0
查看
672
import
I
后退
顶部