-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThread2.cpp
More file actions
147 lines (139 loc) · 4.29 KB
/
Thread2.cpp
File metadata and controls
147 lines (139 loc) · 4.29 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//---------------------------------------------------------------------------
#include <System.hpp>
#include "MainUnit.h"
#include "Book.h"
#pragma hdrstop
#include "Thread2.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// Important: Methods and properties of objects in VCL can only be
// used in a method called using Synchronize, for example:
//
// Synchronize(&UpdateCaption);
//
// where UpdateCaption could look like:
//
// void __fastcall Thread2::UpdateCaption()
// {
// Form1->Caption = "Updated in a thread";
// }
//---------------------------------------------------------------------------
__fastcall Thread2::Thread2(bool CreateSuspended, bool getting, bool inspecting, bool repair, Iterator<PtrBook> *it)
: TThread(CreateSuspended)
{
FreeOnTerminate = true;
NumOfBadConditionBook = 0;
It = it;
RepairBooks = repair;
getingBooks = getting;
inspectingBooks = inspecting;
}
//---------------------------------------------------------------------------
void __fastcall Thread2::Execute()
{
if(getingBooks)
{
srand(time(0));
randomQuantity = rand()%90;
ratio = 100.0/randomQuantity;
Synchronize(&UpdateProgBar1Label);
for( progress = 0; progress<randomQuantity ; progress++)
{
Synchronize(&AddBookToCont);
Sleep(70);
Synchronize(&UpdateProgressBar1);
}
Synchronize(&CompleteGetBooks);
}
else if(inspectingBooks)
{
//Äåêîðàòîð, êîòîðûé ïðîïóñêàåò òîëüêî òå êíèãè, ó êîòîðûõ ñîñòîÿíèå íèæå 50
It = new ConditionDecorator(It, 0, 50);
//Îáíîâëÿåì íàäïèñü, íà÷àëî "îñìîòðà"
Synchronize(&UpdateProgBar2LabelStart);
for(It->First(); !It->IsDone(); It->Next())
{ //Òóò "îñìîòð"
NumOfBadConditionBook++;
}
//Èìèòàöèÿ ïðîãðåññà
ratio = 100.0/NumOfBadConditionBook;
for( progress = 0; progress<NumOfBadConditionBook ; progress++)
{
Sleep(70);
Synchronize(&UpdateProgressBar2);
}
//Îáíîâøÿåì íàäïèñü, êîíåö "îñìîòðà"
Synchronize(&UpdateTableAfterInspect);
}
else if(RepairBooks)
{
//Äåêîðàòîð, êîòîðûé ïðîïóñêàåò òîëüêî òå êíèãè, ó êîòîðûõ ñîñòîÿíèå íèæå 50
It = new ConditionDecorator(It, 0, 50);
Synchronize(&UpdateProgBar3LabelStart);
for(It->First(); !It->IsDone(); It->Next())
{ //Òóò "ðåñòàâðàöèÿ"
It->GetCurrent()->Repair();
NumOfBadConditionBook++;
}
//Èìèòàöèÿ ïðîãðåññà
ratio = 100.0/NumOfBadConditionBook;
for( progress = 0; progress<NumOfBadConditionBook ; progress++)
{
Sleep(100);
Synchronize(&UpdateProgressBar3);
}
//Îáíîâøÿåì íàäïèñü, êîíåö "îñìîòðà"
Synchronize(&UpdateTableAfterRepair);
}
}
//---------------------------------------------------------------------------
void __fastcall Thread2::AddBookToCont()
{
MainForm->AddBookToContainer();
}
//---------------------------------------------------------------------------
void __fastcall Thread2::UpdateProgressBar1()
{
MainForm->ProgressBar1->Position = progress*ratio;
}
void __fastcall Thread2::UpdateProgressBar2()
{
MainForm->ProgressBar2->Position = progress*ratio;
}
void __fastcall Thread2::UpdateProgressBar3()
{
MainForm->ProgressBar3->Position = progress*ratio;
}
void __fastcall Thread2::UpdateProgBar1Label()
{
MainForm->ProgBar1Label->Caption = UnicodeString("Íåñåì êíèãè...");
}
void __fastcall Thread2::CompleteGetBooks()
{
MainForm->ProgressBar1->Position = 100;
MainForm->NumOfBookGet->Caption += UnicodeString(randomQuantity);
MainForm->ProgBar1Label->Caption = UnicodeString("Ãîòîâî");
MainForm->InspectBooks->Enabled = true;
}
void __fastcall Thread2::UpdateTableAfterInspect()
{
MainForm->ProgBar2Label->Caption = UnicodeString("Çàêîí÷èëè");
MainForm->ProgressBar2->Position = 100;
MainForm->BooksToRepair->Caption += UnicodeString(NumOfBadConditionBook);
FillStringTree(MainForm->LibraryStringTree, MainForm->GetIterator());
MainForm->RepairBooks->Enabled = true;
}
void __fastcall Thread2::UpdateTableAfterRepair()
{
MainForm->ProgBar3Label->Caption = UnicodeString("Îòðåñòàâðèðîâàëè");
MainForm->ProgressBar3->Position = 100;
FillStringTree(MainForm->LibraryStringTree, MainForm->GetIterator());
}
void __fastcall Thread2::UpdateProgBar2LabelStart()
{
MainForm->ProgBar2Label->Caption = UnicodeString("Îñìàòðèâàåì...");
}
void __fastcall Thread2::UpdateProgBar3LabelStart()
{
MainForm->ProgBar3Label->Caption = UnicodeString("Ðåñòàâðèðóåì...");
}