Доброго времени суток.
с потоками я вроде как разобрался, теперь проблема в их управлении... данная программа реализует два потока, один на канве хаотично рисует прямоугольники, другой треугольники (на них есть две отдельные кнопки).
ПРОБЛЕМА: при нажатии на одну кнопку нужно при помощи HANDLE (Event) и WaitForSingleObject сделать так, что бы сначала рисовался прямоугольник, потом заданное число треугольников, и только потом следующий прямоугольник и т.д. В идеале треугольники должны вырисовываться в четырёхугольнике, но это я уже их сам туда впихну. Мне главное понять управление потоками. Буду признателен за любую помощь.
Главный Юнит .cpp
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
| #include <vcl.h>
#pragma hdrstop
#include "MainPrimitives.h"
#include "ThrRectangle.h"
#include "ThrTriangle.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFPrimitives *FPrimitives;
ThreadRectangle *ThrRec;
ThreadTriangle *ThrTrng;
//---------------------------------------------------------------------------
__fastcall TFPrimitives::TFPrimitives(TComponent* Owner)
: TForm(Owner)
{
ThrRec=NULL;
ThrTrng=NULL;
}
//---------------------------------------------------------------------------
void __fastcall TFPrimitives::BCloseClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TFPrimitives::BRectangleClick(TObject *Sender)
{
if(ThrRec)
{
ThrRec->Terminate();
delete ThrRec;
ThrRec=NULL;
}
ThrRec = new ThreadRectangle(false);
}
//---------------------------------------------------------------------------
void __fastcall TFPrimitives::BStopClick(TObject *Sender)
{
if(ThrRec)
{
ThrRec->Terminate();
delete ThrRec;
ThrRec=NULL;
ThrTrng->Terminate();
delete ThrTrng;
ThrTrng=NULL;
}
}
//---------------------------------------------------------------------------
void __fastcall TFPrimitives::BClearClick(TObject *Sender)
{
Refresh();
}
//---------------------------------------------------------------------------
void __fastcall TFPrimitives::FormResize(TObject *Sender)
{
Rct.Left=20;
Rct.Top=20;
Rct.Right=ClientWidth-Rct.Left*2;
Rct.Bottom=ClientHeight-Rct.Top*2-(ClientHeight-Panel1->Top);
}
//---------------------------------------------------------------------------
void __fastcall TFPrimitives::BTriangleClick(TObject *Sender)
{
if(ThrTrng)
{
ThrTrng->Terminate();
delete ThrTrng;
ThrTrng=NULL;
}
ThrTrng = new ThreadTriangle(false);
} |
|
.h
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| #ifndef MainPrimitivesH
#define MainPrimitivesH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TFPrimitives : public TForm
{
__published: // IDE-managed Components
TPanel *Panel1;
TButton *BClose;
TButton *BRectangle;
TButton *BTriangle;
TButton *BStop;
TButton *BClear;
void __fastcall BCloseClick(TObject *Sender);
void __fastcall BRectangleClick(TObject *Sender);
void __fastcall BStopClick(TObject *Sender);
void __fastcall BClearClick(TObject *Sender);
void __fastcall FormResize(TObject *Sender);
void __fastcall BTriangleClick(TObject *Sender);
private: // User declarations
public: // User declarations
TRect Rct;
__fastcall TFPrimitives(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TFPrimitives *FPrimitives;
//---------------------------------------------------------------------------
#endif |
|
Поток Четырёхугольников .cpp
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| #include <vcl.h>
#include <stdlib.h>
#pragma hdrstop
#include "ThrRectangle.h"
#include "MainPrimitives.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall ThreadRectangle::ThreadRectangle(bool CreateSuspended)
: TThread(CreateSuspended)
{
randomize();
StepDoneEvent=CreateEvent(NULL, false, false, NULL);
}
//---------------------------------------------------------------------------
__fastcall ThreadRectangle::~ThreadRectangle()
{
CloseHandle(StepDoneEvent);
}
void __fastcall ThreadRectangle::BRectangle()
{
SetEvent(StepDoneEvent);
FPrimitives->Canvas->Pen->Width=5;
FPrimitives->Canvas->Pen->Color=random(0xFFFFFF);
FPrimitives->Canvas->Brush->Style=bsClear;
FPrimitives->Canvas->Rectangle(FPrimitives->Rct.Left+random(FPrimitives->Rct.Width()),
FPrimitives->Rct.Top+random(FPrimitives->Rct.Height()),
FPrimitives->Rct.Left+random(FPrimitives->Rct.Width()),
FPrimitives->Rct.Top+random(FPrimitives->Rct.Height()));
ResetEvent(StepDoneEvent);
}
void __fastcall ThreadRectangle::Execute()
{
while (!Terminated)
{
try
{
Synchronize(BRectangle);
Sleep(1000);
}
catch(Exception &e) {}
}
} |
|
.h
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #ifndef ThrRectangleH
#define ThrRectangleH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class ThreadRectangle : public TThread
{
private:
void __fastcall BRectangle();
protected:
void __fastcall Execute();
public:
HANDLE StepDoneEvent;
__fastcall ThreadRectangle(bool CreateSuspended);
__fastcall ~ThreadRectangle();
};
//---------------------------------------------------------------------------
#endif |
|
Поток Трeугольников .cpp
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| #include <vcl.h>
#include <winbase.h>
#pragma hdrstop
#include "ThrTriangle.h"
#include "MainPrimitives.h"
#include "ThrRectangle.h"
#pragma package(smart_init)
extern ThreadRectangle *ThrRec;
//---------------------------------------------------------------------------
__fastcall ThreadTriangle::ThreadTriangle(bool CreateSuspended)
: TThread(CreateSuspended)
{
randomize();
}
//---------------------------------------------------------------------------
void __fastcall ThreadTriangle::BTriangle()
{
FPrimitives->Canvas->Pen->Width = 2;
FPrimitives->Canvas->Pen->Color = random(0xFFFFFF);
FPrimitives->Canvas->Brush->Style = bsClear;
TPoint p[3];
p[0]=Point(random(FPrimitives->Rct.Width()), random(FPrimitives->Rct.Height()));
p[1]=Point(random(FPrimitives->Rct.Width()), random(FPrimitives->Rct.Height()));
p[2]=Point(random(FPrimitives->Rct.Width()), random(FPrimitives->Rct.Height()));
FPrimitives->Canvas->Polygon(p,2);
}
void __fastcall ThreadTriangle::Execute()
{
while (!Terminated)
{
if(ThrRec && WaitForSingleObject(ThrRec->StepDoneEvent, 20)==WAIT_OBJECT_0)
Synchronize(BTriangle);
//ThreadTriangle=new ThreadTriangle(this, false);
Sleep(400);
}
} |
|
.h
C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| #ifndef ThrTriangleH
#define ThrTriangleH
//---------------------------------------------------------------------------
#include <Classes.hpp>
//---------------------------------------------------------------------------
class ThreadTriangle : public TThread
{
private:
void __fastcall BTriangle();
protected:
void __fastcall Execute();
public:
__fastcall ThreadTriangle(bool CreateSuspended);
};
//---------------------------------------------------------------------------
#endif |
|
Код работает, но не так как хотелось бы... (написано в 5-ом Билдере)