0

I evaluate JavaScript in my Qt application using QScriptEngine::evaluate(QString code). Let's say I evaluate a buggy piece of JavaScript which loops forever (or takes too long to wait for the result). How can I abort such an execution?

I want to control an evaluation via two buttons Run and Abort in a GUI. (But only one execution is allowed at a time.)

I thought of running the script via QtConcurrent::run, keeping the QFuture and calling cancel() when the Abort is was pressed. But the documentation says that I can't abort such executions. It seems like QFuture only cancels after the current item in the job has been processed, i.e. when reducing or filtering a collection. But for QtConcurrent::run this means that I can't use the future to abort its execution.

The other possibility I came up with was using a QThread and calling quit(), but there I have a similar problems: It only cancels the thread if / as soon as it is waiting in an event loop. But since my execution is a single function call, this is no option either.

QThread also has terminate(), but the documentation makes me worry a bit. Although my code itself doesn't involve mutexes, maybe QScriptEngine::evaluate does behind the scenes?

Warning: This function is dangerous and its use is discouraged. The thread can be terminated at any point in its code path. Threads can be terminated while modifying data. There is no chance for the thread to clean up after itself, unlock any held mutexes, etc. In short, use this function only if absolutely necessary.

Is there another option I am missing, maybe some asynchronous evaluation feature?

4

2 に答える 2

1

http://doc.qt.io/qt-4.8/qscriptengine.html#details

あなたの懸念に対処するいくつかのセクションがあります。

http://doc.qt.io/qt-4.8/qscriptengine.html#long-running-scripts

http://doc.qt.io/qt-4.8/qscriptengine.html#script-exceptions

http://doc.qt.io/qt-4.8/qscriptengine.html#abortEvaluation

http://doc.qt.io/qt-4.8/qscriptengine.html#setProcessEventsInterval

それが役立つことを願っています。

于 2013-03-29T22:33:32.993 に答える
0

並行タスク自体は「外部から」中止することはできませんが、QScriptEngine(もちろん、GUI スレッドなどの別のスレッドから) 実行を中止するように指示できます。

QScriptEngine::abortEvaluation(const QScriptValue & result = QScriptValue())

オプションのパラメータは、の呼び出し元に渡される「疑似結果」として使用されますevaluate()

どこかにフラグを設定するか、特別な結果値を使用しabortEvaluation()て、呼び出し元ルーチンが実行が中止されたことを検出できるようにする必要があります。

注: を使用isEvaluating()すると、評価が現在実行されているかどうかを確認できます。

于 2013-03-29T22:31:45.210 に答える