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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
#include "OtherThread.h"
#include <qDebug>
#include <cstdio>
#include "Errors.h"
#ifdef USE_OPENCV
#include <cv.h>
#endif
int OtherThread::count=0;
enum
{
EXC_NONE=0,
EXC_CLASSIC,
EXC_STD,
EXC_CVINTERN,
EXC_CVEXTERN,
};
OtherThread::OtherThread(QObject *parent, QComboBox *eType_, QSpinBox *wait_, QCheckBox *checkBox_)
:QThread(parent), life(true), cbEType(eType_), spWaitTime(wait_), cbStop(checkBox_)
{
count++;
ID=count;
}
void OtherThread::stop()
{
if(isRunning())
{
life=false;
wait();
}
else
{
throw "Can not stop a not running thread";
}
}
void OtherThread::run()
{
MY_FUNCNAME("OtherThread::run()")
int waitTime=spWaitTime->value();
int eType=cbEType->currentIndex();
bool catchStop = cbStop->isChecked();
int count=1, countMax=100;
char msg[50];
sprintf(msg,"#%d: Generate an error",ID);
qDebug() << "#"<<ID<<": Start => " << waitTime << " (ms), " << eType;
while(life)
{
qDebug() << "#"<<ID<<" => "<< count%countMax <<" ("<<count<<")";
try
{
if(count%countMax==0)
{
if(eType==EXC_NONE)
{
qDebug() << "No exception thrown";
}
else if(eType==EXC_CLASSIC)
{
throw "classic exception";
}
else if(eType==EXC_STD)
{
std::vector<int> tab( 10 );
tab.at( 10 ); //Access to 11th elements
}
else if(eType==EXC_CVINTERN)
{
MY_ERROR(msg);
}
else if(eType==EXC_CVEXTERN)
{
#ifdef USE_OPENCV
IplImage *src=cvCreateImage(cvSize(200,200),IPL_DEPTH_8U,3);
src->imageData=0;
cvSet(src,cvScalarAll(0));
#else
throw "OpenCV extern is not defined";
#endif
}
else
{
throw -1;
}
}
}
catch(const std::exception& e)
{
fprintf(stderr,"#%d: %s\n",ID,e.what());
fflush(stderr);
if(catchStop) stop();
}
catch(char * str)
{
fprintf(stderr,"#%d: %s\n",ID,str);
fflush(stderr);
if(catchStop) stop();
}
catch(...)
{
fprintf(stderr,"#%d: Unknown Error\n", ID);
fflush(stderr);
if(catchStop) stop();
}
msleep(waitTime);
count++;
}
qDebug() << "#"<<ID<<": Stop!!!!!!!!";
} |
Partager