[Interest] How to avoid QProcess polling

Bo Thorsen bo at vikingsoft.eu
Thu Apr 16 13:51:20 CEST 2015


On 04/08/2015 09:21 AM, Shantanu Tushar wrote:
> Hi,
>
> Lets say I launch an external application on Windows using this code-
>
> QProcess p;
> p.start("C:\\Windows\\notepad.exe");
>
> Once I run the app and notepad (in this example) is launched, there is
> constant activity happening in my main process (which I found using
> http://technet.microsoft.com/en-in/sysinternals/bb896653.aspx).
>
> I used some profiling tools and found that its because of a QTimer that
> QProcess starts here-
>
>      if (threadData->hasEventDispatcher()) {
>          processFinishedNotifier = new QWinEventNotifier(pid->hProcess, q);
>          QObject::connect(processFinishedNotifier,
> SIGNAL(activated(HANDLE)), q, SLOT(_q_processDied()));
>          processFinishedNotifier->setEnabled(true);
>          notifier = new QTimer(q);
>          QObject::connect(notifier, SIGNAL(timeout()), q,
> SLOT(_q_notified()));
>          notifier->start(NOTIFYTIMEOUT);
>      }
>
> (from
> http://code.qt.io/cgit/qt/qtbase.git/tree/src/corelib/io/qprocess_win.cpp#n542)
>
> Right now I hack around this by doing this-
>
> QThread t;
> QProcess p;
> p.moveToThread(&t);
> p.start("C:\\Windows\\notepad.exe");
>
> which basically causes the hasEventDispatcher() if condition to false
> and no polling happens. However, this generates warnings because the
> QProcess attempts to generate children. Further, I lose the ability to
> read stdout from the process.
>
> Is there a cleaner way to disable the polling? Or at least make it less
> frequent? Just in case you're wondering, this is important because our
> users are freaks about CPU utilization and they don't want to see >0%
> CPU usage when the app is idle from their perspective.

Warning! I have no idea if this is evil or not. Try and google the 
consequences if you want to try it out.

If you really want to get rid of this, you can easily do it:

qDeleteAll(process->findChildren<QTimer>());

Careful though! I don't know if stdout or stderr might fill up here, or 
what might happen inside. This is where you have to figure it out.

You can redirect output to to a file and read it later.

You can also use the findChildren call to find the timer and set the 
timer to something slower.

All this said, I think you should consider if it's really a problem that 
a couple of timers fire in your main application.

Bo Thorsen,
Director, Viking Software.

-- 
Viking Software
Qt and C++ developers for hire
http://www.vikingsoft.eu



More information about the Interest mailing list