Results 1 to 12 of 12

Thread: Is there any way to abort QScriptValue::call() ?

  1. #1
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Is there any way to abort QScriptValue::call() ?

    Hello,
    QScriptValue::call() can block thread. Is there any way to abort QScriptValue::call()? I tried to use QScriptEngine::abortEvaluation(), but this not working. Can anyone help?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is there any way to abort QScriptValue::call() ?

    Are you calling a javascript function or a native one?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there any way to abort QScriptValue::call() ?

    Quote Originally Posted by wysota View Post
    Are you calling a javascript function or a native one?
    I'm calling JS function:
    Qt Code:
    1. function init()
    2. {
    3. for(var i = 0; i < 9000000000; ++i)
    4. for(var j = 0; j < 9000000000; ++j)
    5. var x = 10;
    6. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is there any way to abort QScriptValue::call() ?

    The script engine periodically calls processEvents(), so if you want to abort a call, you would have to do that using an event or as a result of a timer signal being emitted.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there any way to abort QScriptValue::call() ?

    Quote Originally Posted by wysota View Post
    The script engine periodically calls processEvents(), so if you want to abort a call, you would have to do that using an event or as a result of a timer signal being emitted.
    Yes, I know. I do this, but not working!

    In C++ I initialize:
    Qt Code:
    1. engine.setProcessEventsInterval(100);
    2. wait_dialog = new QProgressDialog(tr("Please wait..."), tr("Cancel"), 0, 0);
    3. wait_dialog->setValue(-1);
    4. connect(wait_dialog, SIGNAL(canceled()), SLOT(wait_dialog_canceled()));
    To copy to clipboard, switch view to plain text mode 
    engine is object of QScriptEngine.

    Now I run script:
    Qt Code:
    1. wait_dialog->show();
    2. QScriptValue init_funct = engine.evaluate("init");
    3. init_funct.call(this_object);
    To copy to clipboard, switch view to plain text mode 
    When I click 'Cancel' in wait_dialog then:
    Qt Code:
    1. void sth_engine::wait_dialog_canceled()
    2. {
    3. engine.abortEvaluation();
    4. }
    To copy to clipboard, switch view to plain text mode 
    but 'init_funct.call(this_object);' is still running and blocking thread periodically.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is there any way to abort QScriptValue::call() ?

    Yes, I know. I do this,
    I didn't say anything about clicking any buttons.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there any way to abort QScriptValue::call() ?

    Quote Originally Posted by wysota View Post
    I didn't say anything about clicking any buttons.
    Ok, here is example with timer.

    Initialization:
    Qt Code:
    1. engine.setProcessEventsInterval(100);
    2. this_object = engine.newQObject(this);
    3. timer = new QTimer(this);
    4. timer->setSingleShot(true);
    5. connect(timer, SIGNAL(timeout()), SLOT(timeout()));
    To copy to clipboard, switch view to plain text mode 

    I run script:
    Qt Code:
    1. QScriptValue init_funct = engine.evaluate("init");
    2. timer->start(10000);
    3. init_funct.call(this_object);
    To copy to clipboard, switch view to plain text mode 

    If timer timeout then:
    Qt Code:
    1. void sth::timeout()
    2. {
    3. engine.abortEvaluation();
    4. }
    To copy to clipboard, switch view to plain text mode 
    but after 10 seconds 'init_funct.call(this_object);' is still running and blocking thread periodically.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is there any way to abort QScriptValue::call() ?

    Apparently you are doing something wrong. This works just fine:

    Qt Code:
    1. #include <QScriptEngine>
    2. #include <QtCore>
    3.  
    4. class Object : public QObject {
    5. Q_OBJECT
    6. public:
    7. Object() {
    8. connect(&timer, SIGNAL(timeout()), this, SLOT(abort()));
    9. engine.setProcessEventsInterval(1000);
    10. }
    11. void startEval() {
    12. timer.start(1000);
    13. engine.evaluate("while(true);");
    14. }
    15. private slots:
    16. void abort() {
    17. static int cnt = 0;
    18. bool ev = engine.isEvaluating();
    19.  
    20. qDebug() << "isEvaluating?" << ev;
    21. if(!ev)
    22. timer.stop();
    23. ++cnt;
    24. if(cnt==5) {
    25. qDebug() << "Aborting evaluation";
    26. engine.abortEvaluation();
    27. }
    28. }
    29. private:
    30. QScriptEngine engine;
    31. QTimer timer;
    32. };
    33.  
    34. #include "main.moc"
    35.  
    36. int main(int argc, char **argv){
    37. QCoreApplication app(argc, argv);
    38. Object o;
    39. o.startEval();
    40.  
    41. // return app.exec();
    42. return 0;
    43. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there any way to abort QScriptValue::call() ?

    Quote Originally Posted by wysota View Post
    Apparently you are doing something wrong. This works just fine:
    The topic is: Is there any way to abort QScriptValue::call() ? and you give me example code with QSciptEngine::evaluate().

    Ok, here my full example:
    Qt Code:
    1. #include <QScriptEngine>
    2. #include <QtCore>
    3.  
    4. class Object : public QObject {
    5. Q_OBJECT
    6. public:
    7. Object() {
    8. connect(&timer, SIGNAL(timeout()), this, SLOT(abort()));
    9. engine.setProcessEventsInterval(1000);
    10. this_object = engine.newQObject(this);
    11. }
    12. void startEval() {
    13. engine.evaluate(
    14. "function init()"
    15. "{"
    16. " for(var i = 0; i < 9000000000; ++i)"
    17. " for(var j = 0; j < 9000000000; ++j)"
    18. " var x = 10;"
    19. "}");
    20.  
    21. QScriptValue init_funct = engine.evaluate("init");
    22. timer.start(1000);
    23. init_funct.call(this_object);
    24. qDebug() << "end of startEval()";
    25. }
    26. private slots:
    27. void abort() {
    28. static int cnt = 0;
    29. bool ev = engine.isEvaluating();
    30.  
    31. qDebug() << "isEvaluating?" << ev;
    32. if(!ev)
    33. timer.stop();
    34. ++cnt;
    35. if(cnt==5) {
    36. qDebug() << "Aborting evaluation";
    37. engine.abortEvaluation();
    38. }
    39. }
    40. private:
    41. QScriptEngine engine;
    42. QTimer timer;
    43. QScriptValue this_object;
    44. };
    45.  
    46. #include "main.moc"
    47.  
    48. int main(int argc, char **argv){
    49. QCoreApplication app(argc, argv);
    50. Object o;
    51. o.startEval();
    52.  
    53. // return app.exec();
    54. return 0;
    55. }
    To copy to clipboard, switch view to plain text mode 
    results: "isEvaluating? false" and 'init_funct.call(this_object);' is still running and blocking thread [strike]periodically[/strike].
    Last edited by lukass; 29th February 2012 at 17:30. Reason: [strike]periodically[/strike]

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is there any way to abort QScriptValue::call() ?

    Qt Code:
    1. class Object : public QObject {
    2. Q_OBJECT
    3. public:
    4. Object() {
    5. connect(&timer, SIGNAL(timeout()), this, SLOT(abort()));
    6. engine.setProcessEventsInterval(1000);
    7. fun = engine.evaluate("(function() { while(true); })");
    8. }
    9. void startEval() {
    10. timer.start(1000);
    11. qDebug() << "is callable?" << fun.isFunction();
    12. QScriptContext *context = engine.pushContext();
    13. QScriptValue v = context->activationObject();
    14. v.setProperty("fun", fun);
    15. engine.evaluate("fun()");
    16. engine.popContext();
    17.  
    18. }
    19. private slots:
    20. void abort() {
    21. bool ev = engine.isEvaluating();
    22. qDebug() << "isEvaluating?" << ev;
    23. qDebug() << "Aborting evaluation";
    24. engine.abortEvaluation();
    25. }
    26. private:
    27. // ...
    28. QScriptValue fun;
    29. //...
    30. };
    To copy to clipboard, switch view to plain text mode 

    This calls the function and aborts it after one second. QScriptValue::call() itself doesn't seem abortable as it is executed using different JavaScriptCore method than evaluate(). If you want more flexibility you can probably make your function the activation object itself, set needed arguments and trigger the context (somehow).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. The following user says thank you to wysota for this useful post:

    lukass (29th February 2012)

  12. #11
    Join Date
    Aug 2008
    Posts
    50
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is there any way to abort QScriptValue::call() ?

    Quote Originally Posted by wysota View Post
    This calls the function and aborts it after one second. QScriptValue::call() itself doesn't seem abortable as it is executed using different JavaScriptCore method than evaluate(). If you want more flexibility you can probably make your function the activation object itself, set needed arguments and trigger the context (somehow).
    Thanks, this works!

    However I not fully understand what for is these 4 lines:
    Qt Code:
    1. QScriptContext *context = engine.pushContext();
    2. QScriptValue v = context->activationObject();
    3. v.setProperty("fun", fun);
    4.  
    5. engine.popContext();
    To copy to clipboard, switch view to plain text mode 
    engine.evaluate("fun()"); is enough for me.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is there any way to abort QScriptValue::call() ?

    Quote Originally Posted by lukass View Post
    However I not fully understand what for is these 4 lines:
    Qt Code:
    1. QScriptContext *context = engine.pushContext();
    2. QScriptValue v = context->activationObject();
    3. v.setProperty("fun", fun);
    4.  
    5. engine.popContext();
    To copy to clipboard, switch view to plain text mode 
    Here I register "fun" to be a local property so that when I pop the context, the property is gone. this is to avoid having to register a function I want to call in the global context (as you do). This helps keep the engine clean.

    engine.evaluate("fun()"); is enough for me.
    Yes, because you are calling a function that is a property of the global object. Notice the difference between your code creating the function and mine. In my code the function is anonymous.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. The following user says thank you to wysota for this useful post:

    lukass (1st March 2012)

Similar Threads

  1. How to get QScriptValue property changes
    By enamored in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2012, 06:42
  2. QNetworkAccessManager timeout/abort
    By codeslicer in forum Qt Programming
    Replies: 2
    Last Post: 16th May 2011, 13:20
  3. QTScript - getting the name of a QScriptValue
    By android_ in forum Qt Programming
    Replies: 7
    Last Post: 28th October 2009, 14:24
  4. Abort QDialog in the constructor
    By Tino in forum Qt Programming
    Replies: 2
    Last Post: 29th July 2009, 15:36
  5. QScriptValue with simple typedef types
    By JohannesMunk in forum Newbie
    Replies: 9
    Last Post: 14th May 2009, 15:07

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.