PDA

View Full Version : Multiple QTimer issue



jack249
14th January 2013, 02:20
I've encounter a problem for having 2 timers.
I will use simple code below to explain on it.


static int a = 0;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(&timer_1,SIGNAL(timeout()),this,SLOT(on_timer_1_tim eout()));
connect(&timer_2,SIGNAL(timeout()),this,SLOT(on_timer_2_tim eout()));
timer_1.start(100);
timer_2.start(1000);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_timer_1_timeout()
{
a++;
bool b_out=true;
while(b_out)
{
QCoreApplication::processEvents(); //a breakpoint here
Sleep(500);
}
}

void MainWindow::on_timer_2_timeout()
{
a++;
bool b_out=true;
while(b_out)
{
QCoreApplication::processEvents(); //a breakpoint here
Sleep(500);
}
}

Once i start the program, the program is stop at the breakpoint inside on_timer_1_timeout(), the variable a is 1.
And then i continue the program, the program will stop at the breakpoint inside on_timer_2_timeout(), the variable a is 2.
After that, i keep on continue the program, the program will keep on stuck at on_timer_2_timeout() and the variable a is 2.
What should I do so that the program will back to on_timer_1_timeout() and remain the variable a is 2?
I've try on stating the connection is Qt::QueuedConnection, it is able to go to on_timer_1_timeout() but the variable a will keep on increasing and this is not what I want.

ChrisW67
14th January 2013, 04:50
Can you explain what you are trying to achieve because what you are doing is nonsensical.

jack249
14th January 2013, 06:19
Can you explain what you are trying to achieve because what you are doing is nonsensical.

Ok. I will explain more on it. I have 2 QTimers.
When timer1 is timeout, on_timer_1_timeout() will execute some operation which may take some time.
During the operation, timer2 may timeout, on_timer_1_timeout() will stop at the middle and on_timer_2_timeout() will execute.
I want on_timer_2_timeout() wait until on_timer_1_timeout() execute finish. (I will use a flag variable to indicate the operation finished)
I've tried to use

while(flag)
{
QCoreApplication::processEvents();
Sleep(500);
}
to let the program resume the on_timer_1_timeout() but it seems failed.
The program will stuck in on_timer_2_timeout().
Based on my research, it seems like need to do some multi-threading on it.

ChrisW67
14th January 2013, 07:05
QTimer cannot suspend a running process at an arbitrary point, service another interrupt, and then resume where it left off: this is low-level and real time operating system stuff typically.

It really does not make sense to attach a process that never returns or takes longer than the timer period (you do both) to a timer that fires every 100 or 1000 milliseconds. If the processes are processing queues of repetitive short processing steps then a simple redesign of what is done in response to a timeout may suffice (you might then use QtConcurrent or feed a queue to a worker thread).

wysota
14th January 2013, 12:32
Adding to what Chris said, if you remove calls to processEvents() from your code, the program will sort-of do what you want. timer2 will not timeout before timer1 does its job. However it still doesn't make sense with timer timeouts set so short.