Skip to content

Commit f98cb3e

Browse files
committed
Add timeout arg to execute() and shellExecute()
1 parent 66ccfef commit f98cb3e

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

lib/core/include/qx/core/qx-system.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ QX_CORE_EXPORT bool setDefaultProtocolHandler(const QString& scheme, const QStri
4141
QX_CORE_EXPORT bool isDefaultProtocolHandler(const QString& scheme, const QString& path = {});
4242
QX_CORE_EXPORT bool removeDefaultProtocolHandler(const QString& scheme, const QString& path = {});
4343

44-
QX_CORE_EXPORT ExecuteResult execute(const QString& program, const QStringList& arguments = {});
45-
QX_CORE_EXPORT ExecuteResult shellExecute(const QString& command, const QString& arguments);
44+
QX_CORE_EXPORT ExecuteResult execute(const QString& program, const QStringList& arguments = {}, uint timeout = 2'000);
45+
QX_CORE_EXPORT ExecuteResult shellExecute(const QString& command, const QString& arguments = {}, uint timeout = 2'000);
4646

4747
#ifdef __linux__
4848
// Temporary means to and end, will replace with full parser eventually

lib/core/src/qx-system.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ namespace // Anonymous namespace for effectively private (to this cpp) function
2222

2323
bool isValidScheme(QStringView scheme) { return !scheme.isEmpty() && !scheme.contains(QChar::Space); };
2424

25-
ExecuteResult execute(QProcess& proc)
25+
ExecuteResult execute(QProcess& proc, uint timeout)
2626
{
2727
ExecuteResult res;
2828

2929
proc.start();
30-
if(!proc.waitForStarted(5000))
30+
if(!proc.waitForStarted(1000))
3131
{
3232
res.exitCode = -2;
3333
return res;
3434
}
35-
if(!proc.waitForFinished(5000))
35+
if(!proc.waitForFinished(timeout))
3636
{
3737
proc.kill(); // Force close
3838
proc.waitForFinished();
@@ -274,8 +274,9 @@ bool removeDefaultProtocolHandler(const QString& scheme, const QString& path)
274274
}
275275

276276
/*!
277-
* Starts the program @a program with the arguments @a arguments in a new process, waits for it to finish, and then returns the
278-
* exit code of the process, along with all of it's standard output, from which any trailing line break is removed.
277+
* Starts the program @a program with the arguments @a arguments in a new process, waits for it to finish (or for @a
278+
* timeout to elapse), and then returns the exit code of the process, along with all of it's standard output,
279+
* from which any trailing line break is removed.
279280
*
280281
* The environment and working directory are inherited from the calling process.
281282
*
@@ -289,18 +290,18 @@ bool removeDefaultProtocolHandler(const QString& scheme, const QString& path)
289290
*
290291
* @sa shellExecute().
291292
*/
292-
ExecuteResult execute(const QString& program, const QStringList& arguments)
293+
ExecuteResult execute(const QString& program, const QStringList& arguments, uint timeout)
293294
{
294295
QProcess proc;
295296
proc.setProgram(program);
296297
proc.setArguments(arguments);
297-
return execute(proc);
298+
return execute(proc, timeout);
298299
}
299300

300301
/*!
301302
* Starts the program/command @a command with the arguments @a arguments in a new process using a system shell,
302-
* waits for it to finish, and then returns the exit code of the process, along with all of it's standard output,
303-
* from which any trailing line break is removed.
303+
* waits for it to finish (or for @a timeout to elapse), and then returns the exit code of the process, along
304+
* with all of it's standard output, from which any trailing line break is removed.
304305
*
305306
* This is equivalent to:
306307
* - Windows: `cmd /d /s /c ""command" arguments"`
@@ -318,11 +319,11 @@ ExecuteResult execute(const QString& program, const QStringList& arguments)
318319
*
319320
* @sa execute().
320321
*/
321-
ExecuteResult shellExecute(const QString& command, const QString& arguments)
322+
ExecuteResult shellExecute(const QString& command, const QString& arguments, uint timeout)
322323
{
323324
QProcess proc;
324325
prepareShellProcess(proc, command, arguments);
325-
return execute(proc);
326+
return execute(proc, timeout);
326327
}
327328

328329
}

0 commit comments

Comments
 (0)