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的进程。
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的进程。