C中倒数计时器,不冻结用户输入

时间:2014-04-02 17:52:24

标签: c

我是C的新手。我即将完成一项计划。任何人都可以教我如何在输入内容时制作倒数计时器吗?我目前正在使用sleep(),而且正如我所听到的,它对于线程化很糟糕,并冻结了输入。是吗?

这里的功能是:

void nowPlaying(SONG * h, SONG * t, int * randPrev, int shuffleCon)
{
    SONG * ptr;
    ptr = h->next;
    int random, randCount, mincount = 0, choice = 4, tot = 0, tot2 = 0;

    if(h->next == t) {
        printf("No songs to be played. Add some!\n");
    }
    else if(h->next != t) {
        while(ptr->next != t) {
            tot2 = tot = ((ptr->cdown.minutes*60) + ptr->cdown.secs);
            do {
                printf("------------------------YouTunes------------------------\n");
                printf("========================================================\n");
                printf("TITLE : %s                                            \n", ptr->title);
                printf("ARTIST: %s                                            \n", ptr->artist);
                printf("ALBUM : %s                                            \n", ptr->album);
                switch(ptr->genre) {
                case 1:
                    printf("GENRE : POP                                           \n");
                    break;
                case 2:
                    printf("GENRE : OPM                                           \n");
                    break;
                case 3:
                    printf("GENRE : ROCK                                          \n");
                    break;
                case 4:
                    printf("GENRE : R&B                                           \n");
                    break;
                case 5:
                    printf("GENRE : ACOUSTIC                                      \n");
                    break;
                case 6:
                    printf("GENRE : CLASSICAL                                     \n");
                    break;
                }

                while(tot2 >= 60) {
                    tot2 = tot2 - 60;
                    mincount++;
                    //if(time_left % 60 == 0) mincount++;
                }

                printf("TIME: ");

                if(mincount < 10)
                    printf("0%d", mincount);
                else
                    printf("%d", mincount);

                printf(":");

                if(tot2 < 10)
                    printf("0%d", tot2);
                else if(tot2 == 60)
                    printf("00");
                else
                    printf("%d", tot2);

                printf("\n========================================================\n");
                printf("[1] Prev                 [0]Exit                  [2] Next\n");
                printf("Choice: ");
                //scanf("%d", &choice);
                //timeout(500);
                tot--;
                tot2 = tot;
                mincount = 0;
                sleep(1);
                system("clear");

                if(shuffleCon == 0) {
                    if(choice == 1) {
                        if(ptr->prev == h) {
                            //do nothing
                        }
                        else if(ptr->prev != h) {
                            ptr = ptr->prev;
                        }
                    }
                    else if(choice == 2) {
                        if(ptr->next == t) {
                            //do nothing
                        }
                        else if(ptr->next != t) {
                            ptr = ptr->next;
                        }
                    }
                }
                else if(shuffleCon == 1) {
                    if(choice == 1) {
                        random = shuffle(h, t, randPrev);
                        randCount = 0;
                        ptr = h->next;
                        while(randCount != random) {
                            ptr = ptr->next;
                            randCount++;
                        }
                    }
                    else if(choice == 2) {
                        random = shuffle(h, t, randPrev);
                        randCount = 0;
                        ptr = h->next;
                        while(randCount != random) {
                            ptr = ptr->next;
                            randCount++;
                        }

                        if(ptr == t) {
                            ptr = ptr->prev;
                            ptr = ptr->prev;
                        }
                    }
                }
            } while(tot != -1);

            if(shuffleCon == 0)
                ptr = ptr->next;
            else if(shuffleCon == 1) {
                random = shuffle(h, t, randPrev);
                randCount = 0;
                ptr = h->next;
                while(randCount != random) {
                    ptr = ptr->next;
                    randCount++;
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

从广义上讲,其中一个:

  1. 使用SIGALRM(参见alarm手册页),依靠信号中断系统调用,期待用户输入。

  2. (更好,当然也是一个更有价值的编程练习),编写一个带有selectpoll的事件循环,并将超时设置为超时之前的剩余时间。

  3. 对于您所谈论的那种应用,(2)将是更好的选择。