123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- #define _CRT_SECURE_NO_WARNINGS
- #include <Windows.h>
- #include <stdio.h>
- #include <malloc.h>
- struct ThreadArgs {
- int hours;
- int minutes;
- int seconds;
- int milisec;
- };
- struct ThreadArgs globalTime;
- CRITICAL_SECTION section = { 0 }; //Êðèòè÷åñêàÿ ñåêöèÿ
- int flag = 0;
- int flagSecund = 0;
- int flagTimer = 0;
- DWORD WINAPI PrintTime(LPVOID param) {
- while (1) {
- if (flag) {
- printf("Âðåìÿ îáû÷íîå: %d:%d:%d\n", globalTime.hours, globalTime.minutes, globalTime.seconds);
- flag = 0;
- }
- }
- }
- DWORD WINAPI PrintSecundTime(LPVOID param) {
- struct ThreadArgs* secundStruct = (struct ThreadArgs*)param;
- while (1) {
- if (flagSecund) {
- printf("Ñåêóíäîìåð: %d:%d:%d:%d\n", secundStruct->hours, secundStruct->minutes, secundStruct->seconds, secundStruct->milisec);
- flagSecund = 0;
- }
- }
- }
- DWORD WINAPI PrintTimer(LPVOID param) {
- struct ThreadArgs* timerStruct = (struct ThreadArgs*)param;
- while (1) {
- switch (flagTimer)
- {
- case 1:
- printf("Òàéìåð: %02d:%02d:%02d\n", timerStruct->hours, timerStruct->minutes, timerStruct->seconds);
- flagTimer = 0;
- break;
- case 2:
- printf("ÒÀÉÌÅÐ ÇÀÊÎÍ×ÈËÑß\n");
- ExitThread(1);
- default:
- break;
- }
- }
- }
- DWORD WINAPI TimerCommon(LPVOID param) {
- while (1) {
- globalTime.seconds++;
- if (globalTime.seconds >= 60) {
- globalTime.seconds = 0;
- globalTime.minutes++;
- if (globalTime.minutes >= 60) {
- globalTime.minutes = 0;
- globalTime.hours++;
- if (globalTime.hours >= 24) {
- globalTime.hours = 0;
- }
- }
- }
- flag = 1;
- Sleep(1000);
- }
- }
- DWORD WINAPI Secundomer(LPVOID param) {
- LARGE_INTEGER frequency, start, end;
- // Ïîëó÷àåì ÷àñòîòó ñ÷åò÷èêà ïðîèçâîäèòåëüíîñòè
- QueryPerformanceFrequency(&frequency);
- // Çàïèñûâàåì íà÷àëüíîå âðåìÿ
- QueryPerformanceCounter(&start);
- struct ThreadArgs* secundStruct = (struct ThreadArgs*)param;
- while (1) {
- QueryPerformanceCounter(&end);
- // Âû÷èñëÿåì âðåìÿ, ïðîøåäøåå ñ íà÷àëà ðàáîòû ñåêóíäîìåðà
- LONGLONG elapsed = end.QuadPart - start.QuadPart;
- LONGLONG elapsed_ms = (elapsed * 1000) / frequency.QuadPart;
- secundStruct->hours = (int)(elapsed_ms / (1000 * 60 * 60));
- secundStruct->minutes = (int)((elapsed_ms % (1000 * 60 * 60)) / (1000 * 60));
- secundStruct->seconds = (int)((elapsed_ms % (1000 * 60)) / 1000);
- secundStruct->milisec = (int)(elapsed_ms % 1000);
- flagSecund = 1;
-
- Sleep(1);
- }
- }
- DWORD WINAPI Timer(LPVOID param) {
- struct ThreadArgs* timerStruct = (struct ThreadArgs*)param;
- while (1) {
- timerStruct->seconds--;
- flagTimer = 1;
- if (timerStruct->seconds == 0 && timerStruct->minutes == 0 && timerStruct->hours == 0) {
- flagTimer = 2;
- ExitThread(0);
- }
- else if (timerStruct->seconds == 0) {
- timerStruct->seconds = 60;
- timerStruct->minutes--;
- if (timerStruct->minutes == 0) {
- timerStruct->minutes = 60;
- timerStruct->hours--;
- }
- }
- Sleep(1000);
- }
- }
- //DWORD WINAPI Timer(LPVOID param) {
- // /*struct ThreadArgs* args = (struct ThreadArgs*)param;
- // while (1) {
- // if (paused) {
- // continue;
- // }
- // else {
- // if (timerMode == 0) {
- // printf("Âðåìÿ: %02d:%02d:%02d\n", args->hours, args->minutes, args->seconds);
- // args->seconds++;
- // if (args->seconds >= 60) {
- // args->seconds = 0;
- // args->minutes++;
- // if (args->minutes >= 60) {
- // args->minutes = 0;
- // args->hours++;
- // if (args->hours >= 24) {
- // args->hours = 0;
- // }
- // }
- // }
- // Sleep(1000);
- // }
- // else if (timerMode == 1) {
- // printf("Âðåìÿ: %02d:%02d:%02d\n", args->hours, args->minutes, args->seconds);
- // args->seconds++;
- // if (args->seconds >= 60) {
- // args->seconds = 0;
- // args->minutes++;
- // if (args->minutes >= 60) {
- // args->minutes = 0;
- // args->hours++;
- // }
- // }
- // Sleep(1000);
- //
- // }
- // else if (timerMode == 2) {
- // printf("Âðåìÿ: %02d:%02d:%02d\n", args->hours, args->minutes, args->seconds);
- // if (args->hours <= 0 && args->minutes <= 0 && args->seconds <= 0) {
- // printf("\nÒàéìåð çàêîí÷èëñÿ\n");
- // SYSTEMTIME st;
- // GetLocalTime(&st);
- // args->hours = st.wHour;
- // args->minutes = st.wMinute;
- // args->seconds = st.wSecond;
- // timerMode = 0;
- // }
- // args->seconds--;
- // if (args->seconds <= 0 && !(args->hours <= 0 && args->minutes <= 0 && args->seconds <= 0)) {
- // args->seconds = 60;
- // args->minutes--;
- // if (args->minutes <= 0 && !(args->hours <= 0 && args->minutes <= 0 && args->seconds <= 0)) {
- // args->minutes = 60;
- // args->hours--;
- //
- // }
- // }
- // Sleep(1000);
- //
- // }
- // }
- //
- // }
- // ExitThread(0);*/
- //}
- int main() {
- system("chcp 1251>nul");
- SYSTEMTIME st;
- GetLocalTime(&st);
- globalTime.hours = st.wHour;
- globalTime.minutes = st.wMinute;
- globalTime.seconds = st.wSecond;
- HANDLE hFMain[2];
- HANDLE hFSecund[2];
- HANDLE hFTimer[2];
- int choise = 0;
- printf("Äëÿ îñòàíîâêè âðåìåíè íàïèøèòå 1\nÄëÿ âîçîáíîâëåíèÿ âðåìåíè íàïèøèòå 7\nÄëÿ íàñòðîéêè âðåìåíè íàæìèòå 3\nÄëÿ ðåæèìà ñåêóíäîìåðà íàæìèòå 4, ÷òîáû âûéòè íàæìèòå 5\nÄëÿ ðåæèìà òàéìåðà íàæìèòå 6\n");
- InitializeCriticalSection(§ion);
- struct ThreadArgs secundStruct = { 0, 0, 0, 0 };
- struct ThreadArgs timerStruct = { 0, 0, 0, 0 };
- hFMain[0] = CreateThread(NULL, 0, TimerCommon, NULL, 0, 0);
- hFMain[1] = CreateThread(NULL, 0, PrintTime, NULL, 0, 0);
- while (1) {
- scanf("%d", &choise);
- switch (choise)
- {
- case 1:
- SuspendThread(hFMain[1]);
- SuspendThread(hFMain[0]);
- break;
- case 7:
- ResumeThread(hFMain[0]);
- ResumeThread(hFMain[1]);
- break;
- case 4:
- hFSecund[0] = CreateThread(NULL, 0, Secundomer, &secundStruct, 0, 0);
- hFSecund[1] = CreateThread(NULL, 0, PrintSecundTime, &secundStruct, 0, 0);
- break;
- case 5:
- TerminateThread(hFSecund[0], 0);
- TerminateThread(hFSecund[1], 1);
- printf("Âû çàñåêëè %02d:%02d:%02d:%04d\n", secundStruct.hours, secundStruct.minutes, secundStruct.seconds, secundStruct.milisec);
- struct ThreadArgs secundStruct = { 0, 0, 0, 0 };
- break;
- case 6:
- printf("Ââåäèòå ÷àñû:\n");
- scanf("%d", &timerStruct.hours);
- printf("Ââåäèòå ìèíóòû:\n");
- scanf("%d", &timerStruct.minutes);
- printf("Ââåäèòå ñåêóíäû:\n");
- scanf("%d", &timerStruct.seconds);
- hFTimer[0] = CreateThread(NULL, 0, Timer, &timerStruct, 0, 0);
- hFTimer[1] = CreateThread(NULL, 0, PrintTimer, &timerStruct, 0, 0);
- struct ThreadArgs timerStruct = { 0, 0, 0, 0 };
- break;
- case 3:
- SuspendThread(hFMain[1]);
- SuspendThread(hFMain[0]);
- printf("Ââåäèòå ÷àñû:\n");
- scanf("%d", &globalTime.hours);
- printf("Ââåäèòå ìèíóòû:\n");
- scanf("%d", &globalTime.minutes);
- printf("Ââåäèòå ñåêóíäû:\n");
- scanf("%d", &globalTime.seconds);
- ResumeThread(hFMain[0]);
- ResumeThread(hFMain[1]);
- default:
- break;
- }
- }
- /*while (1) {
- int input;
- scanf("%d", &input);
- if (input == 1) {
- paused = 1;
- }
- else if (input == 0) {
- paused = 0;
- }
- else if (input == 3) {
- paused = 1;
- printf("Ââåäèòå ÷àñû");
- scanf("%d", &args.hours);
- printf("Ââåäèòå ìèíóòû");
- scanf("%d", &args.minutes);
- printf("Ââåäèòå ñåêóíäû");
- scanf("%d", &args.seconds);
- paused = 0;
- }
- else if (input == 4) {
- timerMode = 1;
- paused = 1;
- args.hours = 0;
- args.minutes = 0;
- args.seconds = 0;
- paused = 0;
- }
- else if (input == 5) {
- paused = 1;
- printf("\nÂû çàñåêëè - %d:%d:%d\n", args.hours, args.minutes, args.seconds);
- GetLocalTime(&st);
- args.hours = st.wHour;
- args.minutes = st.wMinute;
- args.seconds = st.wSecond;
- timerMode = 0;
- paused = 0;
- }
- else if (input == 6) {
- paused = 1;
- printf("Ââåäèòå ÷àñû äëÿ òàéìåðà");
- scanf("%d", &args.hours);
- printf("Ââåäèòå ìèíóòû äëÿ òàéìåðà");
- scanf("%d", &args.minutes);
- printf("Ââåäèòå ñåêóíäû äëÿ òàéìåðà");
- scanf("%d", &args.seconds);
- timerMode = 2;
- paused = 0;
- }
- }*/
- WaitForMultipleObjects(2, hFMain, TRUE, INFINITE);
- DeleteCriticalSection(§ion);
- }
|