You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Friday, February 3rd 2012, 9:04am

Running an infinite loop and also processing timer events in console app

I am creating a console application which has a priority queue. I have a class downloadManager running an infinite loop which processes the items as they are added to the queue. I want to add some items to queue based on the timer event. Now the problem is that if I call app.exec() after creating the downloadmanager object, my event processing will never start. I also cannot call exec before starting the infinite loop in the downloadManager object. How can I process timer events in downloadManager object while running it infinite loop. I am new to QT. I know I am doing something wrong here but what, I am not sure. Please help.

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
int main(int argc, char *argv[])
{
	QCoreApplication a(argc, argv);


	FloDownloadManager downloadMngr;
	downloadMngr.startDownload();	

	return a.exec();
}

FloDownloadManager::FloDownloadManager()
{
   ...

	timer = new QTimer();
	this->connect(timer, SIGNAL(timeout()), SLOT(addAdsUpdate()), Qt::DirectConnection);
	timer->start(1000);
}

void FloDownloadManager::run()
{
	forever
	{
    	{
        	//wait here for new item to be added to queue
       	QMutexLocker locker(&mutex);

       	if(downloadQueue.count() == 0)
           	updateAdded.wait(&mutex);
       	
      	//dequeue item from queue and pre-empt if higher priority update is added

     	}

     	//do something
	}
}

2

Friday, February 3rd 2012, 12:02pm

just put your manager in a QThread
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.