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. Why Qt UI Application goes to 'Not Responding' state while the job has already moved to a worker thread?
Qt 6.11 is out! See what's new in the release blog

Why Qt UI Application goes to 'Not Responding' state while the job has already moved to a worker thread?

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 3.0k 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.
  • B BAHRAMUDIN ADIL

    @jsulm I am not accessing the UI from the thread, if you look carefully you will see that everything passing from the worker thread are using signals and slot, otherwise, the application will not start.

    setVisiblePg(true)
    

    this a signal which set the progress bar visible or hides it when accessing from the inside worker thread.

    ui->progressBar->setMaximum(total);
    

    This not from other thread. Look Carefully please!

    J.HilkJ Offline
    J.HilkJ Offline
    J.Hilk
    Moderators
    wrote on last edited by
    #9

    @BAHRAMUDIN-ADIL said in Why Qt UI Application goes to 'Not Responding' state while the job has already moved to a worker thread?:

    @jsulm I am not accessing the UI from the thread, if you look carefully you will see that everything passing from the worker thread are using signals and slot,

    that's a lie

    QThread *thread = QThread::create([this]() {
                int p1 = 0;
                int p2 = 0;
                int p3 = 0;
                int p4 = 0;
                int p5 = 0;
                int p6 = 0;
                int p7 = 0;
                int p8 = 0;
                int p9 = 0;
    
                QString txt = ui->tfTestNumber->text(); //<<<<----------------
                for (int i(0); i<data.size(); ++i) {
                    // ....... loop through data
                    emit setValue(i); // refresh progressBar
                }
    

    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


    Q: What's that?
    A: It's blue light.
    Q: What does it do?
    A: It turns blue.

    B 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      @BAHRAMUDIN-ADIL besides what @jsulm said,
      you're wantonly negligent with other aspects of a thread as well.

      For example you access datawithout a mutex! Data can be changed at any time or it might crash when both threads try to access the container.
      if you're using cpp17 pass your create arguments!

      QThread *thread = QThread::create(myFunction, arg1, arg2);

      Or use QtConCurrent the static function call on run allows arguments as well
      also I see no reentrant checks. Multiple threaded functions could do exactly the same on the same data without mutex locks.

      Depending on how fast your "calculation" is and how big the data.size() you're also flooding the main thread with signals, that can easily overwhelm it.

      B Offline
      B Offline
      BAHRAMUDIN ADIL
      wrote on last edited by
      #10

      @J.Hilk In the first, I don't need mutex because only one thread can access the data at the same time, my data is very big maybe 200,000,000.

      But I don't know this: you're also flooding the main thread with signals, that can easily overwhelm it

      If I don't use signal and slots, then what should I use to communicate between threads?
      Thanks!

      jsulmJ J.HilkJ 2 Replies Last reply
      0
      • B BAHRAMUDIN ADIL

        @J.Hilk In the first, I don't need mutex because only one thread can access the data at the same time, my data is very big maybe 200,000,000.

        But I don't know this: you're also flooding the main thread with signals, that can easily overwhelm it

        If I don't use signal and slots, then what should I use to communicate between threads?
        Thanks!

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #11

        @BAHRAMUDIN-ADIL You're emitting 10.000.000 signals, right? See "I am just generating 10,000,000 records of data".
        If so, then you should reduce the amount of UI updates (your progress dialog). There is really no need to do it for each record.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • B BAHRAMUDIN ADIL

          @J.Hilk In the first, I don't need mutex because only one thread can access the data at the same time, my data is very big maybe 200,000,000.

          But I don't know this: you're also flooding the main thread with signals, that can easily overwhelm it

          If I don't use signal and slots, then what should I use to communicate between threads?
          Thanks!

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #12

          @BAHRAMUDIN-ADIL signal&slots is the way you should go.

          Simply reduce the number of emits, via a modulo check for example, or a QElapsedTimer and only emit every 10 ms, etc.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          2
          • J.HilkJ J.Hilk

            @BAHRAMUDIN-ADIL said in Why Qt UI Application goes to 'Not Responding' state while the job has already moved to a worker thread?:

            @jsulm I am not accessing the UI from the thread, if you look carefully you will see that everything passing from the worker thread are using signals and slot,

            that's a lie

            QThread *thread = QThread::create([this]() {
                        int p1 = 0;
                        int p2 = 0;
                        int p3 = 0;
                        int p4 = 0;
                        int p5 = 0;
                        int p6 = 0;
                        int p7 = 0;
                        int p8 = 0;
                        int p9 = 0;
            
                        QString txt = ui->tfTestNumber->text(); //<<<<----------------
                        for (int i(0); i<data.size(); ++i) {
                            // ....... loop through data
                            emit setValue(i); // refresh progressBar
                        }
            
            B Offline
            B Offline
            BAHRAMUDIN ADIL
            wrote on last edited by
            #13

            @J.Hilk You mean this:

            emit setValue(i); // refresh progressBar
            

            ??
            It is a signal not directly set the value of the progressbar.
            If I directly set it will throw an exception.

            jsulmJ 1 Reply Last reply
            0
            • B BAHRAMUDIN ADIL

              @J.Hilk You mean this:

              emit setValue(i); // refresh progressBar
              

              ??
              It is a signal not directly set the value of the progressbar.
              If I directly set it will throw an exception.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #14

              @BAHRAMUDIN-ADIL This is where you're accessing UI class:

              QString txt = ui->tfTestNumber->text();
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              B 1 Reply Last reply
              1
              • jsulmJ jsulm

                @BAHRAMUDIN-ADIL This is where you're accessing UI class:

                QString txt = ui->tfTestNumber->text();
                
                B Offline
                B Offline
                BAHRAMUDIN ADIL
                wrote on last edited by
                #15

                @jsulm this is just getting the text, not setting text, and also it will be called the only once.

                jsulmJ 1 Reply Last reply
                0
                • B BAHRAMUDIN ADIL

                  @jsulm this is just getting the text, not setting text, and also it will be called the only once.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #16

                  @BAHRAMUDIN-ADIL You still should avoid it. It may work or not.
                  And as already suggested: reduce the amount of signals you're sending for UI updates.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  B 2 Replies Last reply
                  0
                  • jsulmJ jsulm

                    @BAHRAMUDIN-ADIL You still should avoid it. It may work or not.
                    And as already suggested: reduce the amount of signals you're sending for UI updates.

                    B Offline
                    B Offline
                    BAHRAMUDIN ADIL
                    wrote on last edited by
                    #17

                    @jsulm Yes it will make sense, I will do this and see what happens.

                    1 Reply Last reply
                    0
                    • jsulmJ jsulm

                      @BAHRAMUDIN-ADIL You still should avoid it. It may work or not.
                      And as already suggested: reduce the amount of signals you're sending for UI updates.

                      B Offline
                      B Offline
                      BAHRAMUDIN ADIL
                      wrote on last edited by
                      #18

                      @jsulm Thanks! The problem is just with emitig the signals very quickly.

                      1 Reply Last reply
                      2

                      • Login

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