PDA

View Full Version : how to show busy wait when one funtcion is executing



sanjeet
10th November 2011, 11:22
Hi everybody,

In my application, i am using third party function, that is taking more time for executing(approx 10 sec).
For this duration i have to show busy wait, mean processing is going on and blocking the rest of the application.

i have tried QProcessDialog for this but it's not successful.

QProgressDialog progressDialog("Processing...", "Abort", 0, INT_MAX, this);
QProgressBar* bar = new QProgressBar(&progressDialog);
bar->setRange(0, 0);
bar->setValue(0);
progressDialog.setBar(bar);

progressDialog.setMinimumWidth(320);
progressDialog.setMinimumDuration(0);
progressDialog.setWindowModality(Qt::WindowModal);
progressDialog.setValue(0);

//QCoreApplication::processEvents();

progressDialog.setValue(progressDialog.value() + 1);

thirdPartyFunction(); // this function taking much time.

progressDialog.close();

It's not working, mean it's even not displaying progress dialog, but if i am using QCoreApplication::processEvents(); then progress dialog
is displaying but not updating.
I have even tried with QThread, but not became success.

Can you explain me anybody, how to solved this problem ?
My problem is whenever i am using some long operating function, i have to show busy wait in foreground.

Santosh Reddy
11th November 2011, 03:19
Why are you mixing QProgressBar and QProgressDialog, when you can do it just on of them
following works just as you want.

#include <QtGui>


int main(int argc, char *argv[])
{
QApplication a(argc, argv);


QProgressDialog d;
d.setMaximum(50);
d.setValue(10);
d.show();
QApplication::processEvents();


while(1); //or som other long function

d.setValue(50);


return a.exec();
}

sanjeet
11th November 2011, 06:50
Hi Santosh,
First of all very thanks to suggesting me.......
i have mixed progress bar with progress dialog because i have to show "Busy Wait", mean
something gif image type of thing rotating. If i was not adding progress bar separately then
i am unable to show busy wait. When i am adding progress bar, setting range(0,0) , then progress
indicator moving back and forth........ and it seem to "busy wait" something going on in background.

can you guide me so that i can show busy wait. problem is that function is third party API, we haven't
code of these function.
Even i am trying to implement by posix thread or and QThread......... i am not able to find this solution.

Santosh Reddy
13th November 2011, 05:13
#include <QtGui>


class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0)
: QThread(parent){}


signals:
void dataReceived(const QString& text);


protected:
void run(void)
{
while(1)
{
int i;
msleep(100); //do some long function here, third party API
emit dataReceived(QString("%1").arg(i++));
}
}
};


int main(int argc, char *argv[])
{
QApplication a(argc, argv);


QPlainTextEdit w;
MyThread thread;


w.setMaximumBlockCount(1000);
w.connect(&thread, SIGNAL(dataReceived(QString)), &w, SLOT(appendPlainText(QString)));
w.showMaximized();


thread.start();
thread.moveToThread(&thread);


return a.exec();
}


#include "main.moc"

sanjeet
14th November 2011, 12:43
Thank you Santosh, for coding,
it's very beautiful and small code in compare to what i was doing.
i was using low level POSIX threads.

But there is still a problem, i have reached on same position, in both way
either by POSIX thread or by QThread.
Now both thread running parallely...., even busy wait is showing.

But problem is my thread execution is not finishing, It's not a problem in thread.
because that API(third party function) has stopped execution in mead way, after giving
some output on console.

I think it's not getting time slot to finish it's execution. When i was trying with POSIX thread,
i have even used mutex to lock and unlock that section(that API), but when using mutex, it's
giving strange behaviour, without unlocking that section it's returning to main thread. And
creating "system -hang" type of situation.

where as i observed......... now it's problem of thread synchronization, i have to find out mechanism
for proper thread synchronization either by semaphore or mutex. i am trying but didn't get it.

Can you tel me it's problem of thread synchronization ? If Yes, then how to sort out either by QThread
or by POSIX thread.

Santosh Reddy
15th November 2011, 01:31
But problem is my thread execution is not finishing, It's not a problem in thread.
because that API(third party function) has stopped execution in mead way, after giving
some output on console.

As far I understand your problem, you don't have any problem with threads. You have a problem reading the API output.

It does not matter for thread whether API has finished normally or it has returned due to some internal error. All you need to do is to check the return value of the API and update the GUI accordingly.

If this is not the case or if I have not understood your problem, please explain the how you are trying to use the API.

sanjeet
16th November 2011, 13:45
Hi Santosh

Yes you are right, i think too it's not a problem of thread.
What's happening when that API(third party function) using in my application, it's stuck, mean it's not finishing execution,(stoping the execution in mid-way).
I have used the API, where you have suggest me in your demo code. And API will return any value when it will finish the execution either, but it's not coming out of that API.

But when same API, i have tried with only C, in one thread the api is executing and in another thread something printing in console it's working properly. As, API has written in C. I am using (extern "C").

i am giving you both demo code:


C-Code:

#include <unistd.h>
#include <sys/types.h>
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <string.h>
#include <stdlib.h>

void *tfunc(void *arg);
char msg[]="Hai Thread testing";
int run_var=1;
int selection=0;

int main()
{
pthread_t thread;
int res,count=0;
void *thr_result;
res=pthread_create(&thread,NULL,tfunc,(void *)msg);
if(res!=0){printf("\n thread failed\n");exit(0);}
while(count < 30)
{
printf("main\n");
count++;
sleep(1);
}
res=pthread_join(thread,&thr_result);
if(res!=0){printf("\n Join failed\n");exit(0);}
return 0;
}
void *tfunc(void *arg)
{
int count=0;
printf("\n running thread %s\n",(char *)arg);
sleep(1);
strcpy(msg,"Exiting");
printf("in thread\n");

third_party_function(); // this is my third party API

pthread_exit("THank u");
}


Now my Qt-Application code:
i am calling qt-thread in one of my slot.

myclass.h


class MyClass : public QDialog
{
Q_OBJECT
public:
MyClass(QWidget *parent = 0);
private:
QPushButton *startButton;
private slots:
int executionSlot();

};

class MyThread : public QThread
{
Q_OBJECT
public:
explicit MyThread(QObject *parent = 0)
: QThread(parent){}
signals:
void dataReceived();
protected:
void run();
};

myclass.cpp


void MyThread::run()
{
qDebug("thread running now");
while(1)
{
qDebug("In thread....before API");
int ret = third_party_function(); // this is my third party API

// If in place of API, i am using this for loop, for debugging, it's working fine
/*
for(int i=0; i<10; i++){
qDebug("opening modem");
sleep(1);
} */

//If i am using API, it's not coming here.
qDebug("In thread....after API");

emit dataReceived();
break;
}

qDebug("thread finishing now");
}


int MyClass::executionSlot()
{
qDebug("creating prog dlg");

prgDialog prgDlg; //This dialog i have created for showing busy wait proccessing, animation type

MyThread thread;

prgDlg.connect(&thread, SIGNAL(dataReceived()), &prgDlg, SLOT(close()));

thread.start();

thread.moveToThread(&thread);

prgDlg.exec();

qDebug("exiting slot...");

return 0;
}

I have not written full code, because it's very big code, i have just written that section of code where i am getting problem. When i am using that for-loop for some time then it's working fine properly, mean processing dialog appear and after finishing that thread, processing dialog will disappear too . But when i am using the API, processing dialog appears, thread started to run too, but when it's reaching the API, it's not coming out of that API and so processing dialog too running infinite.
But this is okay with c-code.
If you are getting my problem by this .... can you suggest me where i am doing wrong. Thank you in advance !

Santosh Reddy
17th November 2011, 01:56
I hope API is not depending on anything from the Qt part of application.

Is API multi-threaded internally? Then refer it's documentation for any initial configuration for it work properly. Try debugging the API if possible.

It should very trivial for this to work QThread when it already works on pthread. Are you sure it works with pthread (the code you posted)?