简体   繁体   English

如何在C中同时打印两个功能?

[英]How can I print two functions at the same time in C?

Im doing a game in C on console application and I need to print a timer while the user play the game at the same time, I don't know how to program that well, actually I just start using this compiler so I don't know a lot of multithreads and stuff but this is the far as I get, here is an example of two functions the first one is the "timer" and the second its just some question, my doubt is if I can run these 2 functions at the same time, and how I can print the timer while I can answer those 2 question without interrupting the console. 我在控制台应用程序上用C做游戏,我需要在用户同时玩游戏时打印计时器,我不知道如何编程那么好,实际上我只是开始使用这个编译器所以我不知道很多多线程和东西,但这是我得到的,这是两个函数的例子,第一个是“计时器”,第二个只是一些问题, 我的疑问是,如果我可以运行这两个函数在同时,我可以打印计时器,而我可以在不中断控制台的情况下回答这两个问题。

EXAMPLE: 例:

#include <windows.h>
#include <stdio.h>
#include <time.h>


DWORD WINAPI Timer(int limit)
{
    int secs = 1;
    time_t unix;

    struct tm * timeinfo;
    time(&unix);
    timeinfo = localtime(&unix);

    int t1 = timeinfo->tm_sec;
    int t2 = timeinfo->tm_sec;
    int i = 0;

    while(1 == 1)
    {
       time(&unix);
       timeinfo = localtime(&unix);
       if((t1 + i)  == timeinfo->tm_sec)
       {
              system("cls");
              printf("time left %d seconds\n", timeinfo->tm_sec - t2 - limit);
              i++;
       }
       if(timeinfo->tm_sec >= (t1 + limit))
       {
            break;
            printf("Your time its done");
       }
    }
    return 0;
}

DWORD WINAPI Questionary()
{
    puts("testing\n\n");

    int age, height;
    printf("Please write your age: ");
    scanf("%d", &age);
    printf("Please write your height: ");
    scanf("%d", &height);
    printf("\n\nThe numbers written are %d y %d", age, height);
    return NULL;
}
int main()
{
    int i, limit;
    HANDLE tempo;
    HANDLE questions;
    DWORD ThreadId;

    printf("\nHow much time would you like for your timer countdown? ");
    scanf("%d", &limit);

    //Funcion Handle
    questions= CreateThread(NULL,0,Questionary,1,0,&ThreadId);
    WaitForSingleObject(preguntas,INFINITE);

    tempo= CreateThread(NULL,0,Timer(limit),1,0,&ThreadId);
    WaitForSingleObject(tiempofinal,limit*40);


    return 0;
}

You can print the question, and instead of waiting for an answer in a blocking call (like scanf ) you loop while polling for new input, and use ANSI escape codes to print the time at another position. 您可以打印问题,而不是等待阻塞调用(如scanf )中的答案,而是在轮询新输入时循环,并使用ANSI转义码在另一个位置打印时间。

Something like the following pseudo-code: 像下面的伪代码:

print(question)
while (current_time < timer_end)
{
    if (have_input())
        break

    save_current_cursor_position()
    move_cursor_position(x, y)
    print(timer)
    restore_saved_cursor_position()

    very_short_sleep()
}

What you want with threads, can't be accomplished with the line oriented functions, something like ncurses would be necessary for multithreaded console read/write operation. 您想要的线程,无法通过面向行的函数来完成,多线程控制台读/写操作需要像ncurses这样的东西。

What Joachim Pileborg, wrote is your best chance, the limitation is that your clock can't be ticking while you wait for input., if you poll every 1 second, writing the time and then waiting for the answer, most humans would not be able to reply on 1 second, so every 10 seconds would perhaps be the most sensible. Joachim Pileborg写的是你最好的机会,限制是你等待输入时你的时钟不能滴答作响。如果你每1秒轮询一次,写下时间然后等待答案,大多数人都不会能够在1秒内回复,所以每10秒也许是最明智的。

An alternate method to present the time ticking every second while still polling every second would be to save the input of that second on a accumulative buffer, and rewrite it on the screen on next input polling, besides the time ticking. 在每秒仍然轮询的情况下每隔一秒显示时间滴答的另一种方法是将该秒的输入保存在累积缓冲区上,并在下一次输入轮询时在屏幕上重写它,除了时间滴答。 This does have the danger of missing some input done while the loops alternate and is more complicated to code. 这确实存在在循环交替时丢失一些输入并且编码更复杂的危险。

This is a case where a Graphical interface would be better suited for your needs. 这种情况下,图形界面更适合您的需求。

Y seria buena idea que traduzcas al inglés tus nombres de variables para ayudar a una más rápida comprensión del contenido de tu programa, ya que es lo que entiende la mayoría por acá. Y seria buena idea que traduzcasalingléstusnombres de variables para ayudar aunamásrápidacomprensióndelcontenido de tu programa,ya que es lo que entiende lamayoríacapá。

As i know there is no way to write to console from different threads at the same time. 据我所知,无法同时从不同的线程写入控制台。 Altough it looks two thread are writing at the same time, actually one of them that write at a time (Which catch the console to write). 它看起来两个线程正在同时写入,实际上其中一个是一次写入(其中一个是抓住控制台写的)。 In this way altough two thread is running at the same time, it does not make sense. 通过这种方式,两个线程同时运行,它没有意义。

I think you can solve your problem as a little tricky in one thread but it is not not good. 我认为你可以在一个线程中解决你的问题,但这并不是一件好事。 But i think you can gui components and from two different threads, you can set field of this component and this satisfy your requirement. 但我认为你可以gui组件,从两个不同的线程,你可以设置这个组件的字段,这满足您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM