简体   繁体   English

按住滚动条会在Windows中暂停命令提示符

[英]Holding scroll-bar gets command prompt to pause in Windows

I have a program where I record data through an ADC system from National Instruments (NI). 我有一个程序,我通过NI(NI)的ADC系统记录数据。

The device buffers information for some time, and then the program collects the buffer data at some point. 设备缓冲信息一段时间,然后程序在某个时刻收集缓冲区数据。 If the program collects data larger than the buffer, then the buffer would have to free without my program receiving the data, which will cause the NI library to throw an exception saying that requested data isn't available anymore, since it was lost. 如果程序收集的数据大于缓冲区,则缓冲区必须释放,而我的程序不会接收数据,这将导致NI库抛出一个异常,说明所请求的数据不再可用,因为它已丢失。

Since my program is a command-prompt program, if the user clicks and holds the scrollbar, the program pauses, which could get this problem to happen. 由于我的程序是一个命令提示程序,如果用户单击并按住滚动条,程序会暂停,这可能会导致此问题发生。

How can I get over this problem without increasing the buffer size? 如何在不增加缓冲区大小的情况下解决此问题? Can I disable this holding thing in Windows? 我可以在Windows中禁用此保存功能吗?

Thanks. 谢谢。

Only the thread that is attempting to output to the console is blocked. 仅阻止尝试输出到控制台的线程。 Make this a separate thread, and your problem goes away. 将它作为一个单独的线程,你的问题就会消失。

Of course, you'll need to buffer up your output, and do something sensible if the buffer overflows. 当然,你需要缓冲输出,并在缓冲区溢出时做一些合理的事情。

For reference, here's the simple code I used to test this, you will note that the counter continues to increase even when the scroll bar is held down: 作为参考,这是我用来测试它的简单代码,你会注意到,即使按住滚动条,计数器也会继续增加:

#include <Windows.h>
#include <stdio.h>

volatile int n = 0;

DWORD WINAPI my_thread(LPVOID parameter)
{
    for (;;)
    {
        n = n + 1;
        Sleep(800);
    }
}

int main(int argc, char ** argv)
{
    if (!CreateThread(NULL, 0, my_thread, NULL, 0, NULL))
    {
        printf("Error %u from CreateThread\n", GetLastError());
        return 0;
    }
    for (;;)
    {
        printf("Hello!  We're at %u\n", n);
        Sleep(1000);
    }
    return 0;
}

Whilst there may be ways to bypass each individual problem you can possibly conceive with the output [including for example running it over a network on a sometimes slow output link, or some such], I think the correct thing to do is to disconnect your output from your collecting of data. 虽然可能有办法绕过每个单独的问题,你可以设想输出[包括例如通过网络在有时很慢的输出链接上运行它,或者某些这样的],我认为正确的做法是断开你的输出从你收集数据。 It shouldn't be hard to do this by adding a separate thread that collects the data, and having the main thread display to the command prompt window. 通过添加一个收集数据的单独线程,并将主线程显示到命令提示符窗口,这应该不难。 That way, not matter which variation of "output is blocked" Windows throws at you, it will work - at least until you run out of RAM, but tat that point it's YOUR program's decision to do something [eg throw away some data or some such]. 这样,无论Windows输出的“输出被阻止”的哪种变化,它都会起作用 - 至少在你的RAM耗尽之前,但是这表明你的程序决定做某事[例如扔掉一些数据或某些数据]这样]。

This is generally how the problem "I need to collect something, and I also need to allow users to view the data, but I don't want the two to interfere with each other" is solved. 这通常是问题“我需要收集什么,我还需要允许用户查看数据,但我不希望这两者互相干扰”得到解决。

First use the GetConsoleWindow winapi function and get the HWND of your console. 首先使用GetConsoleWindow winapi函数并获取控制台的HWND。 now i suggest two ways to do this, 现在我建议两种方法来做到这一点,

Method I Subclass the window by creating your own WindowProcedure. 方法I通过创建自己的WindowProcedure对窗口进行子类化。 ( get help from here ) Now that you have subclassed it, you can intercept the WM_VSCROLL and WM_HSCROLL messages and do your own remedy to your code. 从这里获得帮助 )现在您已经将其子类化了,您可以拦截WM_VSCROLL和WM_HSCROLL消息并对您的代码进行自己的补救。

Method II Change the size of the window using some function like SetWindowPos so that the scroll bars are not needed. 方法II使用SetWindowPos等函数更改窗口大小,以便不需要滚动条。 or Change the size of the console screen buffer so that the scroll bars are not needed. 或更改控制台屏幕缓冲区的大小,以便不需要滚动条。

Method I has lot of control over the application, but its a little bit complex than the method II which is very simple. 方法I对应用程序有很多控制权,但它比方法II有点复杂,非常简单。 If you want to forbid the user from resizing the console window, just remove the WS_THICKFRAME from the WindowStyle of the console window. 如果要禁止用户调整控制台窗口的大小,只需从控制台窗口的WindowStyle中删除WS_THICKFRAME

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

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