|
3
Win 32 API.
-
2007
- 3 .
: .., . .
:
, .
() , 2007
: Win32 API, , , .
3
. - Image. Image , - . . , , Image, .
:
- , , , ,
- : , .
- / ,
- ( ), ( ),
- , .
Win32 API. , (, SidorovThread).
3
( , ) A, B, C (.2) 1, 2, 3, (.3) D (. 4) E (1, 2, 3, , . 5), F (f1, f2, f3, , . 6). , . , . . 7.
1
.
|
A
|
C 1
|
E1
|
F1
|
B
|
C2
|
E2
|
F2
|
C
|
C3
|
E3
|
F 3
|
D
|
01
| 02
|
1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| 13
| 14
| 15
|
1
| 30
| 1
| 1
| 2
| 1
| 3
| 2
| 2
| 3
| 5
| 1
| 2
| 1
| 1
|
2
| 29
| 2
| 1
| 2
| 2
| 3
| 2
| 3
| 4
| 5
| 2
| 2
| 1
| 2
|
3
| 28
| 3
| 1
| 3
| 3
| 4
| 2
| 1
| 1
| 5
| 3
| 2
| 1
| 3
|
4
| 27
| 4
| 1
| 1
| 1
| 4
| 2
| 1
| 1
| 6
| 1
| 2
| 1
| 1
|
5
| 26
| 5
| 1
| 2
| 1
| 5
| 3
| 2
| 1
| 6
| 2
| 6
| 1
| 2
|
6
| 25
| 6
| 1
| 6
| 1
| 5
| 3
| 2
| 1
| 6
| 3
| 7
| 1
| 3
|
7
| 24
| 7
| 1
| 2
| 1
| 6
| 3
| 2
| 1
| 7
| 1
| 2
| 1
| 1
|
8
| 23
| 8
| 1
| 3
| 1
| 6
| 3
| 6
| 1
| 7
| 2
| 2
| 1
| 2
|
9
| 22
| 9
| 1
| 7
| 1
| 7
| 3
| 2
| 1
| 7
| 3
| 2
| 1
| 3
|
10
| 21
| 10
| 1
| 2
| 2
| 7
| 1
| 2
| 1
| 8
| 1
| 3
| 1
| 1
|
11
| 20
| 1
| 2
| 2
| 1
| 8
| 1
| 3
| 1
| 8
| 2
| 3
| 1
| 2
|
12
| 19
| 2
| 2
| 4
| 4
| 8
| 1
| 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
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
|
, , , , ( 31). , , Image, . , , . . , . 32 ( 6 . 7). : / , / , , , , .
1. .
2. (.1):
3. : , , .
void __fastcall TForm1::FormCreate(TObject *Sender)
{
sync = CheckBox7->Checked; //
randomize();
a[0] = THREAD_PRIORITY_LOWEST; clr[0] = clRed;
a[1] = THREAD_PRIORITY_BELOW_NORMAL; clr[1] = clBlue;
a[2] = THREAD_PRIORITY_NORMAL; clr[2] = clGreen;
a[3] = THREAD_PRIORITY_ABOVE_NORMAL; clr[3] = clAqua;
a[4] = THREAD_PRIORITY_HIGHEST; clr[4] = clYellow;
for(int i=0;i<5;i++){ //
ComboBox1->Items->Add(a[i]);
ComboBox2->Items->Add(a[i]);
ComboBox3->Items->Add(a[i]);
ComboBox4->Items->Add(a[i]);
ComboBox5->Items->Add(a[i]);
}
ComboBox1->ItemIndex = 2;
ComboBox2->ItemIndex = 2;
ComboBox3->ItemIndex = 2;
ComboBox4->ItemIndex = 2;
ComboBox5->ItemIndex = 2;
SYSTEM_INFO SI;
GetSystemInfo(&SI);
ProcCount = SI.dwNumberOfProcessors;
Caption = Caption + " : "+IntToStr(ProcCount)+")";
w = Form1->Image1->Width; h = Form1->Image1->Height;
InitializeCriticalSection(&CS1); //
InitializeCriticalSection(&CS2);
}
4. StartThread, . , . , fl_wait. , WaitAndClose, , WaitAndClose.
DWORD WINAPI StartThread(void)
{
int i;
NCP = StrToInt(Form1->Edit1->Text); //
fl_write_file = Form1->CheckBox8->Checked; // true =
if(fl_write_file)
hFile = CreateFile("test.txt",GENERIC_WRITE,FILE_SHARE_WRITE,
NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
ClearBox1(); ClearBox2(); //
fl_wait = Form1->CheckBox6->Checked; //
// ,
// , ,
// . ,
// - .
// , , // .
// 32
for(i=0;i<n_pot1;i++){ //
hThread1[i] = CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)SidorovThread,(void *) i, 0, &ID);
}
WaitAndClose(0); //
ClearBox1(); ClearBox2();
for(i=0;i<n_pot2;i++){ //
hThread2[i] = CreateThread(NULL,0,
(LPTHREAD_START_ROUTINE)SidorovThread,(void *)(3+i),0,&ID);
}
WaitAndClose(1); //
ClearBox1(); ClearBox2(); //
Form1->Button1->Enabled = true; //
}
5. WaitAndClose . 0 1, , (fl_wait = false), , . , / .
void WaitAndClose(int i)
{
switch(i){
case 0: Form1->Button1->Enabled = false;
Form1->Label6->Caption = " ";
Form1->CheckBox1->Enabled = true;
Form1->CheckBox2->Enabled = true;
Form1->CheckBox3->Enabled = true;
Form1->CheckBox4->Enabled = false;
Form1->CheckBox5->Enabled = false;
if(fl_wait)WaitForMultipleObjects(n_pot1,hThread1,TRUE,INFINITE);
Form1->CheckBox4->Enabled = true;
Form1->CheckBox5->Enabled = true;
Form1->CheckBox1->Enabled = false;
Form1->CheckBox2->Enabled = false;
Form1->CheckBox3->Enabled = false;
break;
case 1: Form1->Label6->Caption = " ";
if(fl_wait)WaitForMultipleObjects(n_pot2,hThread2,TRUE,INFINITE);
Form1->Label6->Caption = " ";
Form1->CheckBox4->Enabled = false;
Form1->CheckBox5->Enabled = false;
break;
}
}
6. , . : . . , , , Ris1 Ris2, . , , , , .
DWORD WINAPI SidorovThread(int pot)
{
int i,a = 5;
float x[3], y[3], vx[3], vy[3];
for(i=0;i<3;i++){ // ,
x[i] = Random(w); y[i] = Random(h);
vx[i] = 0.001+0.05*Random(10); vy[i] = 0.001+0.05*Random(10);
Sleep(5);
}
fl_exit = false; //
for(i=0;i<NCP;i++){ //
if(fl_exit) ExitThread(0);
sync = Form1->CheckBox7->Checked;
Ris1(x,y,vx,vy,clr[pot]); //
Ris2(pot,(w*i)/NCP,clr[pot]); //
}
}
7. random SidorovThread, , .. , , , . , .
int Random (int nMaxValue)
{
// random :
// .
// Randomize .
return(abs(nMaxValue*cos(GetCurrentThreadId()*GetTickCount())));
}
8. Ris1 Ris2. Ris1 : , . ( ), . ( ) draw1. draw1 . Ris2 , . , draw2. , . Sleep(0) Ris1, Ris2 . , , , , , .. , .
void Ris1(float x[3],float y[3], float vx[3], float vy[3], TColor c)
{
if(sync){
EnterCriticalSection(&CS1);
draw1(clSilver,x,y);
LeaveCriticalSection(&CS1);
}
else draw1(clSilver,x,y);
for(int i=0;i<3;i++){
x[i]+=vx[i]; y[i]+=vy[i];
if((x[i]>w) || (x[i]<0)){
vx[i]=-vx[i]; x[i]+=vx[i];
}
if((y[i]>h) || (y[i]<0)){
vy[i]=-vy[i]; y[i]+=vy[i];
}
}
if(sync){
EnterCriticalSection(&CS1);
draw1(c,x,y);
LeaveCriticalSection(&CS1);
}
else draw1(c,x,y);
Sleep(0);
}
//------------
void Ris2(int pot, int i,TColor c)
{
if(sync){
EnterCriticalSection(&CS2);
draw2(pot,i,c);
LeaveCriticalSection(&CS2);
}
else draw2(pot,i,c);
Sleep(0);
}
9. , draw1 draw2 . , Image, - WriteComToFile.
void draw1(TColor c, float x[3], float y[3])
{
if(!fl_write_file){ //
Form1->Image1->Canvas->Pen->Color = c;
Form1->Image1->Canvas->Brush->Color = c;
Form1->Image1->Canvas->MoveTo(x[2],y[2]);
for(int i=0;i<3;i++){
Form1->Image1->Canvas->LineTo(x[i],y[i]);
Form1->Image1->Canvas->Rectangle(x[i],y[i],x[i]+5,y[i]+5);
}
Form1->Image1->Refresh();
}
else{ //
WriteComToFile('c',c,0,0,0,1);
WriteComToFile('m',x[2],y[2],0,0,1);
for(int i=0;i<3;i++){
WriteComToFile('l',x[i],y[i],0,0,1);
WriteComToFile('r',x[i],y[i],x[i]+5,y[i]+5,1);
}
WriteComToFile('f',0,0,0,0,1);
}
}
//----------------
void draw2(int pot, int i,TColor c)
{
if(!fl_write_file){ //
Form1->Image2->Canvas->Pen->Color = c;
Form1->Image2->Canvas->MoveTo(i,10+pot*5);
Form1->Image2->Canvas->LineTo(i,15+pot*5);
Form1->Image2->Refresh();
}
else{ //
WriteComToFile('c',c,0,0,0,2); //
WriteComToFile('m',i,10+pot*5,0,0,2); // LineTo...
WriteComToFile('l',i,15+pot*5,0,0,2); // MoveTo...
WriteComToFile('f',0,0,0,0,2); // Refresh
}
}
10. WriteComToFile. : , , (1 Image1, 2 Image2). : - , l - LineTo, m - MoveTo, r - rectangle, f - Refresh, s Sleep. , , , , f , s , l m , r - .
void WriteComToFile(char com,int a,int b,int c,int d,int no)
{
//
// : : 1, 2, 3, 4,
// ;/n
// : - , l - LineTo, m - MoveTo,
// r - rectangle, f - Refresh, s - Sleep
CHAR ToFile[64];
sprintf(ToFile,"%c: %d, %d, %d, %d %d\n",com,a,b,c,d,no);
WriteFile(hFile,ToFile,strlen(ToFile),&RealWriteByte,NULL);
}
11. , , . : ( ), . StrCom, , (.. , ), : FromFile (64 ), : StrCom. . .
void __fastcall TForm1::Button5Click(TObject *Sender)
{
DWORD nb1;
int a,b,c,d,ob;
char com;
TCanvas *Cnv;
CHAR FromFile[64];
CloseHandle(hFile);
hFile = CreateFile("test.txt",GENERIC_READ,0,NULL,
OPEN_EXISTING,0,NULL);
StrCom = "";
do{
ReadFile(hFile,FromFile,sizeof(FromFile),&nb1,NULL);
for(int i=0;i<nb1;i++){
if(FromFile[i]!='\n') StrCom += FromFile[i];
else{ // -
sscanf(StrCom.c_str(),"%c: %d, %d, %d, %d %d\n",
&com,&a,&b,&c,&d,&ob);
StrCom = "";
if(ob == 1) Cnv = Form1->Image1->Canvas;
else Cnv = Form1->Image2->Canvas;
switch(com){
case 'c': Cnv->Pen->Color = a; break;
case 'l': Cnv->LineTo(a,b); break;
case 'm': Cnv->MoveTo(a,b); break;
case 'r': Cnv->Rectangle(a,b,c,d); break;
case 'f': Form1->Image1->Refresh();
Form1->Image2->Refresh(); break;
case 's': Sleep(a); break;
}
}
}
}while(nb1!=0);
CloseHandle(hFile);
}
12. . , StartThread:
void __fastcall TForm1::Button4Click(TObject *Sender)
{
StartThread(); //
}
, , StartThread CreateThread, .. :
void __fastcall TForm1::Button1Click(TObject *Sender)
{
// .
// .
SThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)StartThread,
NULL,0,&ID);
}
13. :
void __fastcall TForm1::ComboBox1Change(TObject *Sender)
{
SetThreadPriority(hThread1[0],a[ComboBox1->ItemIndex]);
}
14. , RadioGroup:
void __fastcall TForm1::RadioGroup1Click(TObject *Sender)
{
SetThreadIdealProcessor(hThread1[0],RadioGroup1->ItemIndex+1);
SetThreadAffinityMask(hThread1[0],RadioGroup1->ItemIndex+1);
}
15. / . , . , , , , .
void __fastcall TForm1::CheckBox1Click(TObject *Sender)
{
EnterCriticalSection(&CS1); // ,
EnterCriticalSection(&CS2); // .
if(CheckBox1->Checked) SuspendThread(hThread1[0]);
else ResumeThread(hThread1[0]);
ClearBox1();
LeaveCriticalSection(&CS2);
LeaveCriticalSection(&CS1);
}
16. ClearBox1, ClearBox2 .
void ClearBox1(void)
{
Form1->Image1->Canvas->Brush->Color = clSilver;
Form1->Image1->Canvas->Rectangle(0,0,w,h);
}
//-------
void ClearBox2(void)
{
Form1->Image2->Canvas->Brush->Color = clSilver;
Form1->Image2->Canvas->Rectangle(0,0,w,h);
}
17. , :
void __fastcall TForm1::Button3Click(TObject *Sender)
{
fl_exit = true;
}
18. ( ): , 0,1 , , , .
void __fastcall TForm1::Button2Click(TObject *Sender)
{
fl_exit = true;
Sleep(100);
DeleteCriticalSection(&CS1); //
DeleteCriticalSection(&CS2);
CloseHandle(SThread);
CloseHandle(hFile);
Close();
}
: 2018-10-15; !; : 194 |
:
:
, .
==> ...
1843 - | 1644 -
© 2015-2024 lektsii.org - -
: 0.357 .