Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to stop a function in one thread?
Forum Updated to NodeBB v4.3 + New Features

How to stop a function in one thread?

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 1.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MonkeyTang
    wrote on 5 Dec 2013, 13:59 last edited by
    #1

    Here's my case:
    I have one function, which will be a long time running,not a loop. But so many steps to do in such a function. So I move this test function to one thread. And also, I need this test function stop immediately when stop button clicked in the main thread, no matter which step runs in the test function. So I do this purpose bellow:

    @
    (init thread)
    threadDaqCtrl = new QThread();
    daqValveCtrl->moveToThread(threadDaqCtrl);
    @
    @
    (when stop button clicked, terminate this thread,the function in this thread must stop.and restart this thread,wait for second calling.)
    threadDaqCtrl->terminate();
    threadDaqCtrl->start();
    @

    So problem comes:

    1. this action not so good, is there any way better than terminate thread directly?

    2. the program will crash when start button clicked for calling the test function next time. How to solve this?
      (this only happens in win7, XP and win8 are ok,no crash! my platform is: QT4.8.3+VS2010sp1. )

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 Dec 2013, 22:09 last edited by
      #2

      Hi and welcome to devnet,

      Indeed, terminating a thread like that also means that it's state is unknown so you should not reuse it like that.

      I you have such a long function, juste add a check between each step like

      @if (!continue)
      return;
      @

      or something better and add a function to set continue to false when needing to stop. So now your thread can end cleanly and you can restart it.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      1/2

      5 Dec 2013, 13:59

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved