Unsolved Notifications from Stdin on Windows
-
I'm trying to make an application that can process messages sent to stdin without blocking the event loop, on Windows.
I've been trying both QSocketNotifier, initialized as notifier_(STDIN_FILENO,QSocketNotifier::Read,this) and QWinEventNotifier, initialized as notifier_(GetStdHandle(STD_INPUT_HANDLE),this).QSocketNotifier appears to never fire at all. My suspicions are that this is expected behavior in windows, but the docs don't seem to mention its use in conjunction with stdin on windows.
QWinEventNotifier fires constantly, regardless of whether anything is in the stdin buffer or not. The docs do state "The state of the event is not modified in the process, so if it is a manual reset event, you will need to reset it after the notification." but calling ResetEvent on the handle provided by the signal doesn't do anything - the signal still emits continuously.
Am I doing something wrong, or should I just expect QWinEventNotifier to be clogging up my event loop like this? Is there a better way to get signals on windows corresponding to data actually being available from stdin?
I'm not sure what the right way to approach actually reading from stdin from inside this slot is. Since the signal doesn't actually mean there's anything available to read, any attempt to read causes my program to hang inside that slot forever until some data is written. The std::cin.eof() check always reports false even when no data is present. I tried std::cin.rdbuf()->in_avail(), but this always reports 0 even when data is written. QTextStream.atEnd() doesn't seem to work here. If I can't fix the signal to actually mean that data is available, is there at least some kind of means of checking for available data before I start an infinitely hanging read call?
-
What does the STDIN_FILENO macro evaluate to? Is it the handle number, or a pointer to the handle number? Read the docs for the QSocketNotifier constructor carefully.
-
Something along these lines should be working, right?
QFile * file = new QFile; if (!file->open(stdin, QIODevice::ReadOnly)) QTextStream(stdout) << "Can't open the standard input"; QObject::connect(qApp, &QCoreApplication::aboutToQuit, file, &QObject::deleteLater); QObject::connect(file, &QIODevice::readyRead, file, [file] () -> { QTextStream(stdout) << file->readAll(); });
-
The above won't work - QFile never emits readyRead, according to docs.
The perpetually activating QWinEventNotifier remains a mystery - but surely there's some way to read stdin via a QFile that doesn't block forever if there's no data available?
-
@Ananym
Hi
I dont think they ever added that option.
waitForReadyRead seems to do nothing so no easy option to use QFile for stdin.