-4

我正在编写我的应用程序,我需要一个状态菜单来显示验证功能的当前状态。验证功能占用大量 CPU,需要工作,所以我无法一直打印我的状态。目前这是我的代码:

HANDLE t = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)thread_test, 0, 0, 0);
int c;
while (true)
{
    printf("[s]tatus, [e]xit => ");
    c = getch();
    putchar(c);
    if (c == 's') {
        putchar('\n');
        is_visible = true;
    }
    else if (c == 'e')
        ExitProcess(0);
    else putchar('\n');
}

这是代码thread_test()

bool is_visible = false;
void thread_test() {
    while (true) {
        if (is_visible == true) printf("This is status.\n");
    }
}

现在我怎样才能让thread_test()函数只显示一次这个消息然后继续而不显示任何东西?提前致谢。

4

1 回答 1

0

如何使 thread_test() 函数只显示此消息一次,然后继续而不显示任何内容?

建议更换:

if (is_visible == true) printf("This is status.\n"); 

if ( is_visible ) 
{ 
    printf( "This is status.\n" ); 
    is_visible = false; 
}
于 2018-06-17T04:39:17.987 回答