Міністерство освіти і науки, молоді та спорту України
Луцький національний технічний університет
Кафедра
«Комп’ютерної інженерії»
КОНТРОЛЬНА РОБОТА
з курсу «Паралельні та розподілені обчислення»
Луцьк 2011
Задача 1.
Клас TThread
Код програми:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ExtCtrls,
StdCtrls;
type
TMDIWorkThread = class(TThread)
private
Image: TImage;
X1, Y1, X2, Y2: Integer;
Color: TColor;
protected
procedure Execute; override;
procedure DrawLine;
public
constructor Create(AImage: TImage);
end;
type
TForm1 = class(TForm)
Image1: TImage;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
WorkThread: TMDIWorkThread;
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TMDIWorkThread.Execute;
begin
repeat
X1 := Random(Image.Width);
Y1 := Random(Image.Height);
X2 := Random(Image.Width);
Y2 := Random(Image.Height);
Color := TColor(Random($00FFFFFF));
Synchronize(DrawLine);
until Terminated;
end;
procedure TMDIWorkThread.DrawLine;
begin
Image.Canvas.Pen.Color := Color;
Image.Canvas.MoveTo(X1, Y1);
Image.Canvas.LineTo(X2, Y2);
end;
constructor TMDIWorkThread.Create(AImage: TImage);
begin
inherited
Create(True);
Image := AImage;
Priority := TPLowest;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
button1.Enabled := False;
button2.Enabled := True;
WorkThread.Resume;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
button1.Enabled := True;
WorkThread.Terminate;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Application.Terminate;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := CAFree;
WorkThread.Terminate;
WorkThread.Free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DoubleBuffered := True;
WorkThread := TMDIWorkThread.Create(Image1);
end;
end.
Відкомпільована програма має наступний вигляд:
Задача 2.
Призначення потокам пріоритетів
Код програми:
unit Ball;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ImgList, DBCtrls;
type
TBallThread = class(TThread)
private
FBall: TImage;
FPosition: Integer;
protected
procedure Execute; override;
procedure DrawBall;
constructor Create(ABall: TImage; APriority: TThreadPriority);
end;
TBallBox = class(TForm)
Image1: TImage;
Image2: TImage;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
public
end;
var
BallBox: TBallBox;
Ball1, Ball2: TBallThread;
implementation
{$R *.dfm}
constructor TBallThread.Create(ABall: TImage; APriority: TThreadPriority);
begin
inherited Create(True);
FBall := ABall;
FPosition := FBall.Left;
Priority := APriority;
end;
procedure TBallThread.Execute;
var
I: Integer;
begin
repeat
if FPosition > (BallBox.Width - FBall.Width - 10) then
FPosition := 2;
for I := 0 to 1000000 do
FPosition := FPosition + 1;
FPosition := FPosition - 1000000;
Synchronize(DrawBall);
until Terminated;
end;
procedure TBallThread.DrawBall;
begin
FBall.Left := FPosition;
Application.ProcessMessages;
end;
procedure TBallBox.FormCreate(Sender: TObject);
begin
Self.DoubleBuffered := True;
Image1.Picture.LoadFromFile('Blue.bmp');
Image1.AutoSize:=True;
Image1.Transparent:=True;
Image2.Picture.LoadFromFile('Red.bmp');
Image2.AutoSize:=True;
Image2.Transparent:=True;
Ball1 := TBallThread.Create(Image1, tpHighest);
Ball2 := TBallThread.Create(Image2, tpLowest);
end;
procedure TBallBox.FormDestroy(Sender: TObject);
begin
if Ball1.Suspended then Ball1.Resume;
if Ball2.Suspended then Ball2.Resume;
Ball1.Terminate;
Ball2.Terminate;
Ball1.Free;
Ball2.Free;
end;
procedure TBallBox.Button1Click(Sender: TObject);
begin
Ball1.Resume;
Ball2.Resume;
end;
procedure TBallBox.Button2Click(Sender: TObject);
begin
Ball1.Suspend;
Ball2.Suspend;
end;
procedure TBallBox.Button3Click(Sender: TObject);
begin
Ball1.Terminate;
Ball2.Terminate;
if Ball1.Suspended then
Ball1.Resume;
if Ball2.Suspended then
Ball2.Resume;
Close;
end;
end.
Відкомпільована програма має наступний вигляд:
Задача 3.
Багато віконна програма (MDI)
Код програми:
unit Main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, ToolWin;
type
TMBox = class(TForm)
ToolBar3: TToolBar;
NewPage: TToolButton;
procedure NewPageClick(Sender: TObject);
procedure FormShow(Sender: TObject);
private
public
M: Integer;
end;
var
MBox: TMBox;
implementation
uses Child;
{$R *.dfm}
procedure TMBox.FormShow(Sender: TObject);
begin
M := 0;
end;
procedure TMBox.NewPageClick(Sender: TObject);
var
T: TCBox;
begin
T := TCBox.Create(Application);
T.Caption := 'Сторінка - ' + IntToStr(M);
T.Show;
end;
end.
Код програми для модуля Child наведено нижче:
unit Child;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
TMDIWorkThread = class(TThread)
private
Image: TImage;
X1, Y1, X2, Y2: Integer;
Color: TColor;
protected
procedure Execute; override;
procedure DrawLine;
public
constructor Create(AImage: TImage);
end;
type
TCBox = class(TForm)
Image1: TImage;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
private
WorkThread: TMDIWorkThread;
public
end;
var
CBox: TCBox;
implementation
uses Main;
{$R *.dfm}
procedure TMDIWorkThread.Execute;
begin
repeat
X1 := Random(Image.Width);
Y1 := Random(Image.Height);
X2 := Random(Image.Width);
Y2 := Random(Image.Height);
Color := TColor(Random($00FFFFFF));
Synchronize(DrawLine);
until Terminated;
end;
procedure TMDIWorkThread.DrawLine;
begin
Image.Canvas.Pen.Color := Color;
Image.Canvas.MoveTo(X1, Y1);
Image.Canvas.LineTo(X2, Y2);
end;
constructor TMDIWorkThread.Create(AImage: TImage);
begin
inherited
Create(False);
Image := AImage;
Priority := TPLowest;
end;
procedure TCBox.FormClose(Sender: TObject; var Action: TCloseAction);
begin
MBox.M := MBox.M - 1;
Action := CAFree;
WorkThread.Terminate;
WorkThread.Free;
end;
procedure TCBox.FormCreate(Sender: TObject);
begin
DoubleBuffered := True;
WorkThread := TMDIWorkThread.Create(Image1);
end;
procedure TCBox.FormShow(Sender: TObject);
begin
MBox.M := MBox.M + 1;
end;
end.
Відкомпільована програма має наступний вигляд: