CoolVeryVery 2 hafta önce
ebeveyn
işleme
6d0c8ea0e7
1 değiştirilmiş dosya ile 211 ekleme ve 84 silme
  1. 211 84
      FlowWindows/Source.c

+ 211 - 84
FlowWindows/Source.c

@@ -7,112 +7,185 @@ 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) {
+	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);
+		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* args = (struct ThreadArgs*)param;
+	struct ThreadArgs* timerStruct = (struct ThreadArgs*)param;
     while (1) {
-        if (paused) {
-            continue;
+        timerStruct->seconds--;
+        flagTimer = 1;
+        if (timerStruct->seconds == 0 && timerStruct->minutes == 0 && timerStruct->hours == 0) {
+            flagTimer = 2;
+            ExitThread(0);
         }
-        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);
-
+        else if (timerStruct->seconds == 0) {
+            timerStruct->seconds = 60;
+            timerStruct->minutes--;
+            if (timerStruct->minutes == 0) {
+                timerStruct->minutes = 60;
+                timerStruct->hours--;
             }
         }
-        
+        Sleep(1000);
     }
-    ExitThread(0);*/
 }
 
+//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;
@@ -122,16 +195,70 @@ int main() {
     globalTime.hours = st.wHour;
     globalTime.minutes = st.wMinute;
     globalTime.seconds = st.wSecond;
-    HANDLE hF[2];
+    HANDLE hFMain[2];
+    HANDLE hFSecund[2];
+    HANDLE hFTimer[2];
 
+    int choise = 0;
 
-    printf("Äëÿ îñòàíîâêè âðåìåíè íàïèøèòå 1\nÄëÿ îñòàíîâêè âðåìåíè íàïèøèòå 0\nÄëÿ íàñòðîéêè âðåìåíè íàæìèòå 3\nÄëÿ ðåæèìà ñåêóíäîìåðà íàæìèòå 4, ÷òîáû âûéòè íàæìèòå 5\nÄëÿ ðåæèìà òàéìåðà íàæìèòå 6\n");
+    printf("Äëÿ îñòàíîâêè âðåìåíè íàïèøèòå 1\nÄëÿ âîçîáíîâëåíèÿ âðåìåíè íàïèøèòå 7\nÄëÿ íàñòðîéêè âðåìåíè íàæìèòå 3\nÄëÿ ðåæèìà ñåêóíäîìåðà íàæìèòå 4, ÷òîáû âûéòè íàæìèòå 5\nÄëÿ ðåæèìà òàéìåðà íàæìèòå 6\n");
     InitializeCriticalSection(&section);
+    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 };
 
-    hF[0] = CreateThread(NULL, 0, TimerCommon, NULL, 0, 0);
-    hF[1] = CreateThread(NULL, 0, PrintTime, NULL, 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);
@@ -182,7 +309,7 @@ int main() {
             paused = 0;
         }
     }*/
-    WaitForMultipleObjects(2, hF, TRUE, INFINITE);
+    WaitForMultipleObjects(2, hFMain, TRUE, INFINITE);
     DeleteCriticalSection(&section);