Работа с потоками (удалить поток)
26.05.2019, 15:18. Показов 623. Ответов 0
Добрый день, заранее благодарен всем за помощь.
Написать программу, которая запускает новый поток при нажатии левой клавиши мыши. Поток начинает выводить возрастающую числовую последовательность в текущую позицию курсора мыши. При нажатии левой клавиши мыши программа удаляет поток, координаты которого ближе всего к положению мыши.
Как выполнить последнее условие? - При нажатии левой клавиши мыши программа удаляет поток, координаты которого ближе всего к положению мыши.
| 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
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
| #include <windows.h>
#include <process.h>
#include <stdlib.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwnd;
int cxClient, cyClient;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "RndRctMT";
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd = CreateWindow(szAppName, "Program",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
WORD xPos1, yPos1, xPos2, yPos2, xPos3, yPos3, nSize;
TCHAR szBuf1[80], szBuf2[80], szBuf3[80];
int x1 = 0, x2 = 0, x3 = 0;
VOID Thread(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x1++;
nSize = wsprintf(szBuf1, TEXT("(%d)"), x1);
TextOut(hdc, xPos1, yPos1, szBuf1, nSize); Sleep(50);
}
}
ReleaseDC(hwnd, hdc);
}
VOID Thread2(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x2++;
nSize = wsprintf(szBuf2, TEXT("(%d)"), x2);
TextOut(hdc, xPos2, yPos2, szBuf2, nSize); Sleep(50);
}
}
ReleaseDC(hwnd, hdc);
}
VOID Thread3(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x3++;
nSize = wsprintf(szBuf3, TEXT("(%d)"), x3);
TextOut(hdc, xPos3, yPos3, szBuf3, nSize); Sleep(50);
}
}
ReleaseDC(hwnd, hdc);
}
int count = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_CREATE:
return 0;
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
return 0;
case WM_MOUSEMOVE:
break;
case WM_LBUTTONDOWN:
count++;
InvalidateRect(hwnd, NULL, TRUE);// означает, что все окно надо перерисовать
if (count == 1) {
xPos1 = LOWORD(lParam);
yPos1 = HIWORD(lParam);
_beginthread(Thread, 0, NULL);
}
if (count == 2) {
xPos2 = LOWORD(lParam);
yPos2 = HIWORD(lParam);
_beginthread(Thread2, 0, NULL);
}
if (count == 3) {
xPos3 = LOWORD(lParam);
yPos3 = HIWORD(lParam);
_beginthread(Thread3, 0, NULL);
}
break;
case WM_RBUTTONDOWN:
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
} |
|
Добавлено через 1 час 23 минуты
Останавливает сразу два потока, вместо одного.
| 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
| #include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <cmath>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "RndRctMT";
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd = CreateWindow(szAppName, "Random Rectangles",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
WORD xPos1, yPos1, xPos2, yPos2, xPos3, yPos3,xPosKursora, yPosKursora, nSize;
TCHAR szBuf1[80], szBuf2[80], szBuf3[80];
int x1 = 0, x2 = 0, x3 = 0;
bool STOP_PROCESS1 = false;
bool STOP_PROCESS2 = false;
bool STOP_PROCESS3 = false;
VOID Thread(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x1++;
nSize = wsprintf(szBuf1, TEXT("(%d)"), x1);
TextOut(hdc, xPos1, yPos1, szBuf1, nSize); Sleep(50);
if (STOP_PROCESS1 == true) { _endthread(); }
}
}
ReleaseDC(hwnd, hdc);
}
VOID Thread2(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x2++;
nSize = wsprintf(szBuf2, TEXT("(%d)"), x2);
TextOut(hdc, xPos2, yPos2, szBuf2, nSize); Sleep(50);
if (STOP_PROCESS2 == true) { _endthread(); }
}
}
ReleaseDC(hwnd, hdc);
}
VOID Thread3(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x3++;
nSize = wsprintf(szBuf3, TEXT("(%d)"), x3);
TextOut(hdc, xPos3, yPos3, szBuf3, nSize); Sleep(50);
if (STOP_PROCESS3 == true) { _endthread(); }
}
}
ReleaseDC(hwnd, hdc);
}
int count = 0;
int Del_Potok1 = 0, Del_Potok2 = 0, Del_Potok3 = 0;
double Distance1 = 0, Distance2 = 0, Distance3 = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_CREATE:
return 0;
case WM_SIZE:
return 0;
case WM_MOUSEMOVE:
xPosKursora = LOWORD(lParam);
yPosKursora = HIWORD(lParam);
break;
case WM_LBUTTONDOWN:
count++;
InvalidateRect(hwnd, NULL, TRUE);// означает, что все окно надо перерисовать
if (count == 1) {
xPos1 = LOWORD(lParam);
yPos1 = HIWORD(lParam);
_beginthread(Thread, 0, NULL);
}
if (count == 2) {
xPos2 = LOWORD(lParam);
yPos2 = HIWORD(lParam);
_beginthread(Thread2, 0, NULL);
}
if (count == 3) {
xPos3 = LOWORD(lParam);
yPos3 = HIWORD(lParam);
_beginthread(Thread3, 0, NULL);
}
if (count == 4) {
Distance1 = abs( sqrt(pow(xPos1 - xPosKursora, 2) + pow(yPos1 - yPosKursora, 2)));
Distance2 = abs( sqrt(pow(xPos2 - xPosKursora, 2) + pow(yPos2 - yPosKursora, 2)));
Distance3 = abs( sqrt(pow(xPos3 - xPosKursora, 2) + pow(yPos3 - yPosKursora, 2)));
Distance3 = Distance3;
if (Distance1 < Distance2 && Distance3) {
STOP_PROCESS1 = TRUE; count = count - 2;
}
if (Distance2 < Distance1 && Distance3){
STOP_PROCESS2 = TRUE; count = count - 2;
}
if (Distance3 < Distance1 && Distance2) {
STOP_PROCESS3 = TRUE; count = count - 2;
}
}
break;
case WM_RBUTTONDOWN:
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
} |
|
Добавлено через 26 минут
Решение:
| 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
| #include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <cmath>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
HWND hwnd;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static char szAppName[] = "RndRctMT";
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize = sizeof(wndclass);
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
wndclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wndclass);
hwnd = CreateWindow(szAppName, "Random Rectangles",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
WORD xPos1, yPos1, xPos2, yPos2, xPos3, yPos3,xPosKursora, yPosKursora, nSize;
TCHAR szBuf1[80], szBuf2[80], szBuf3[80];
int x1 = 0, x2 = 0, x3 = 0;
bool STOP_PROCESS1 = false;
bool STOP_PROCESS2 = false;
bool STOP_PROCESS3 = false;
VOID Thread(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x1++;
nSize = wsprintf(szBuf1, TEXT("(%d)"), x1);
TextOut(hdc, xPos1, yPos1, szBuf1, nSize); Sleep(50);
if (STOP_PROCESS1 == true) { _endthread(); }
}
}
ReleaseDC(hwnd, hdc);
}
VOID Thread2(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x2++;
nSize = wsprintf(szBuf2, TEXT("(%d)"), x2);
TextOut(hdc, xPos2, yPos2, szBuf2, nSize); Sleep(50);
if (STOP_PROCESS2 == true) { _endthread(); }
}
}
ReleaseDC(hwnd, hdc);
}
VOID Thread3(PVOID pvoid)
{
HDC hdc;
while (true) {
hdc = GetDC(hwnd);
for (int i = 0; i < 1000; i++)
{
x3++;
nSize = wsprintf(szBuf3, TEXT("(%d)"), x3);
TextOut(hdc, xPos3, yPos3, szBuf3, nSize); Sleep(50);
if (STOP_PROCESS3 == true) { _endthread(); }
}
}
ReleaseDC(hwnd, hdc);
}
int count = 0;
int Del_Potok1 = 0, Del_Potok2 = 0, Del_Potok3 = 0;
double Distance1 = 0, Distance2 = 0, Distance3 = 0;
LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
switch (iMsg)
{
case WM_CREATE:
return 0;
case WM_SIZE:
return 0;
case WM_MOUSEMOVE:
xPosKursora = LOWORD(lParam);
yPosKursora = HIWORD(lParam);
break;
case WM_LBUTTONDOWN:
count++;
InvalidateRect(hwnd, NULL, TRUE);// означает, что все окно надо перерисовать
if (count == 1) {
xPos1 = LOWORD(lParam);
yPos1 = HIWORD(lParam);
_beginthread(Thread, 0, NULL);
}
if (count == 2) {
xPos2 = LOWORD(lParam);
yPos2 = HIWORD(lParam);
_beginthread(Thread2, 0, NULL);
}
if (count == 3) {
xPos3 = LOWORD(lParam);
yPos3 = HIWORD(lParam);
_beginthread(Thread3, 0, NULL);
}
if (count == 4) {
Distance1 = abs( sqrt(pow(xPos1 - xPosKursora, 2) + pow(yPos1 - yPosKursora, 2)));
Distance2 = abs( sqrt(pow(xPos2 - xPosKursora, 2) + pow(yPos2 - yPosKursora, 2)));
Distance3 = abs( sqrt(pow(xPos3 - xPosKursora, 2) + pow(yPos3 - yPosKursora, 2)));
Distance3 = Distance3;
if (Distance1 < Distance2 && Distance1 < Distance3) {
STOP_PROCESS1 = TRUE; count = count - 1;
}
if (Distance2 < Distance1 && Distance2 < Distance3){
STOP_PROCESS2 = TRUE; count = count - 1;
}
if (Distance3 < Distance1 && Distance3 < Distance2) {
STOP_PROCESS3 = TRUE; count = count - 1;
}
}
break;
case WM_RBUTTONDOWN:
break;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, iMsg, wParam, lParam);
} |
|
0
|