1

I am writing a console application in Qt that maintains a UDP and some TCP connections. The user is occasionally asked to confirm some information before the application continues its regular outgoing messages. However, the way I currently wait for that user input is not functioning the way I need it to.

For now I'm just using this "simple" construct:

std::cout << "Press Enter to continue...":
std::cin.get();

When writing this I knew it would not really work and was gonna have to be replaced, but I do not know how. While it does pause the further sending of messages, it seemingly pauses the entire program, including the Qt event loop. In turn, that causes all TCP connections to be dropped by the peers, as they don't recieve any answers and will assume the connection is to be closed. UDP datagrams can not be handled either.

So I'm trying to find a way that is following Qt's event-based setup. Alternatively I was considering running this on another thread, though I would like to use a few threads as possible, as the hardware this app is running on is very limited in it's capabilities.

Edit: Thanks for the answers, I went with the base for eyllanesc's answer in the linked question at gist.github.com/gjorquera/2576569 , it seems to be working fine.

Karsten G
  • 11
  • 4
  • You want one part of the program to keep running (Network connection handling) and another part to wait for the user input? I don't see another option than to use two threads... – Mickaël C. Guimarães Aug 25 '23 at 10:51
  • I was hoping there was some way to get and react to events/signals from the console instead of opening more threads. – Karsten G Aug 25 '23 at 11:16
  • I'd consider writing a separate (possibly non-Qt) app, which you launch with QProcess. You can also use a different thread, but terminating that thread cleanly can be problematic at program exit. Killing another process is probably more robust. – hyde Aug 26 '23 at 10:15
  • If you are on Linux/Mac, you should be able to use QSocketNotifier on file number 0. Sadly that does not work on Windows. – hyde Aug 26 '23 at 10:16
  • See this https://stackoverflow.com/questions/59336957/chrome-addon-is-randomly-sending-trash-to-my-c-native-host-application/59348696#59348696 – eyllanesc Aug 26 '23 at 15:24
  • Thanks for the answers, I went with the base for eyllanesc's answer in the linked question at https://gist.github.com/gjorquera/2576569 , it seems to be working fine. – Karsten G Aug 28 '23 at 12:55

0 Answers0