Holding the scrollbar causes the command prompt to pause in Windows

I have a program in which I am writing data through an ADC system from National Instruments (NI).

The device buffers information for some time, and then the program collects the buffer data at some point. If the program collects data that exceeds the buffer, then the buffer will be freed without my program receiving the data, which will cause the NI library to throw an exception stating that the requested data is no longer available because it was lost.

Since my program is a command line program, if the user presses and holds the scroll bar, the program pauses, which can cause this problem.

How can I overcome this problem without increasing the buffer size? Can this feature be disabled on Windows?

Thank.

+5


source to share


4 answers


Only the thread that is trying to output to the console is blocked. Make it a separate thread and your problem goes away.

Of course, you will need to buffer your output and do something sane if the buffer overflows.



For reference, here's the simple code I used to test this, you will notice that the counter keeps increasing even when you hold down the scrollbar:

#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;
}

      

+4


source


While there may be ways to get around every single issue you can imagine with the output [including, for example, running it over the network on a sometimes slow output link or some such], I think the right thing to do is disconnect your output from data collection. It shouldn't be hard to do this by adding a separate thread that collects data and shows the main thread in a command prompt window. So, no matter what variation of "exit blocked" Windows throws at you, it will work - at least until you run out of RAM, but if that means your software solution is doing something (eg , throw out some or some of the data].



Typically, the problem is "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."

+3


source


First use GetConsoleWindow winapi function and get HWND of your console. now i suggest two ways to do it,

Method I Subclassing a window by creating your own WindowProcedure. ( get help here ) Now that you've subclassed it, you can intercept the WM_VSCROLL and WM_HSCROLL messages and inject your own code into your code.

Method II Resize the window using some function such as SetWindowPos so that no scrollbars are needed. or Resize the console screen buffer so that scrollbars are not needed.

Method I has a lot of control over the application, but it is a little more complex than Method II, which is very simple. If you want to prevent the user from resizing the console window, simply remove WS_THICKFRAME

the console windows from the WindowStyle window.

+2


source


I was in a similar situation and found that this kind of blocking could be caused by a "quick edit" command line "function". This question explains about this and the answer shows how to disable it. Hope it helps, even after a few years :)

0


source







All Articles