-2

i got problem when i trying to run a working thread in a for loop.

my code is something like this:

connect(&myworkingthread,SIGNAL(updataprocess(int)),processbar2,SLOT(setValue(int)));

    for (int i=0;i<n;i++){

      // each individual data will be loaded in this part...

      myworkingthread.start();// this thread will take 5 secs to finish, a signal is 
    // also emitted to show the process of this thread(processbar2).

     // after working thread, the processed data will be saved... 

      processbar1->setValue(i); // processbar is used to show the processing process

      //problem of this code: data is totally wrong because the next thread will start before the last one finish.

}

i also wants to show the process of myworkingthread which is supposed to be implemented by signal and slot. if i use the above code, the data is totally wrong. because the second thread will starts before the first one finish.

then i change my code like this:

connect(&myworkingthread,SIGNAL(updataprocess(int)),processbar2,SLOT(setValue(int)));
    for (int i=0;i<n;i++){

          // each individual data will be loaded in this part...

          myworkingthread.start();// this thread will take 5 secs to finish
          // signal is also emitted to show the process of this thread(processbar2).
          myworkingthread.wait();// i will wait the thread until it finish

         // after working thread, the processed data will be saved... 

          processbar1->setValue(i); // processbar is used to show the processing process

    }

the problem of this code is that processbar of the thread is not working until the for loop goes through all the files.

is there any way to make the thread process in a for loop?

László Papp
  • 51,870
  • 39
  • 111
  • 135
jiangstonybrook
  • 133
  • 2
  • 9
  • Progressbar( widget ) can be updated with GUI thread only – Ashif Sep 07 '13 at 04:28
  • i use signal and slot mechanism to make it before but without for loop. it can be done. – jiangstonybrook Sep 07 '13 at 04:32
  • useful link..http://stackoverflow.com/questions/2806552/qprogressbar-not-showing-progress – Ashif Sep 07 '13 at 04:34
  • yes. i think i did exactly that link suggested. but the problem is that i have to use thread.wait() function to wait my individual thread finish before the next start which somehow disable the signal mechanism. i just have no idea how to solve this problem – jiangstonybrook Sep 07 '13 at 04:52
  • 1
    I do not follow your problem. Have you followed the suggestions in the other thread for your myworkingthread, i.e. you emit the signal properly? – László Papp Sep 07 '13 at 07:18
  • 1
    Calling start() followed by wait() makes the main thread block which makes using another thread somewhat pointless. – Frank Osterfeld Sep 07 '13 at 10:30

1 Answers1

0

You need to call

QApplication::processEvents();

see Simple example using QProgressDialog: Any ideas why this doesn't work properly? As far as I remenber, this should be called after the "emit" line in your thread...

Community
  • 1
  • 1
Tob
  • 286
  • 1
  • 17