Interrupt 3rd party endless function
-
I have 3rd party function, say
3rdpartyClass::connect(std::string ip, int port), which is being stucked if you pass wrong IP. I want it to abort or return from function which callsconnect(MyClass::init()below) after some time (3 second for example) in case it's still running.I've tried messing with timers and
QThread::terminate()but nothing has worked.Is there a proper way to do what I want somehow?
A bit context:
myclass.h:
class MyClass : public QObject { Q_OBJECT 3rdpartyClass *3rdpartyObj = nullptr; SomeClass *someClassObj; SomeOtherClass *someOtherClassObj; public: MyClass(QObject *parent = nullptr); public slots: void init(); }myclass.cpp:
MyClass::MyClass(QObject *parent) : QObject(parent) { 3rdpartyClass *3rdpartyObj = new 3rdpartyClass(); someClassObj = new SomeClass(); someOtherClassObj = new SomeOtherClass(); } MyClass::init() { 3rdpartyObj->connect("192.168.1.1", 100); //<-------- possible endless loop here }myclassmanager.h:
class MyClassManager : public QObject { Q_OBJECT QThread* workingThread = nullptr; MyClass *myClass = nullptr; public: MyClassManager(QObject *parent = nullptr); }myclassmanager.cpp:
MyClassManager::MyClassManager(QObject *parent) : QObject(parent) { myClass = new MyClass(); workingThread = new QThread(this); workingThread->start(); myClass->moveToThread(workingThread); QMetaObject::invokeMethod(myClass, &MyClass::init); } -
Hi,
Doesn't that third party offer any kind of API for that ?
If so, a possible workaround would be to use QNetworkAccessManager to check whether the wanted host is reachable before starting the connection through your third party.
-
I have 3rd party function, say
3rdpartyClass::connect(std::string ip, int port), which is being stucked if you pass wrong IP. I want it to abort or return from function which callsconnect(MyClass::init()below) after some time (3 second for example) in case it's still running.I've tried messing with timers and
QThread::terminate()but nothing has worked.Is there a proper way to do what I want somehow?
A bit context:
myclass.h:
class MyClass : public QObject { Q_OBJECT 3rdpartyClass *3rdpartyObj = nullptr; SomeClass *someClassObj; SomeOtherClass *someOtherClassObj; public: MyClass(QObject *parent = nullptr); public slots: void init(); }myclass.cpp:
MyClass::MyClass(QObject *parent) : QObject(parent) { 3rdpartyClass *3rdpartyObj = new 3rdpartyClass(); someClassObj = new SomeClass(); someOtherClassObj = new SomeOtherClass(); } MyClass::init() { 3rdpartyObj->connect("192.168.1.1", 100); //<-------- possible endless loop here }myclassmanager.h:
class MyClassManager : public QObject { Q_OBJECT QThread* workingThread = nullptr; MyClass *myClass = nullptr; public: MyClassManager(QObject *parent = nullptr); }myclassmanager.cpp:
MyClassManager::MyClassManager(QObject *parent) : QObject(parent) { myClass = new MyClass(); workingThread = new QThread(this); workingThread->start(); myClass->moveToThread(workingThread); QMetaObject::invokeMethod(myClass, &MyClass::init); }Just to add to @SGaist: I don't think there is a way to abort a function and continue your program (well,with a debugger it would be possible)
You could wrap the 3rdparty API into a separate program, which you start by QProcess. You could then abort the program after some timeout.
Regards
-
Hi,
Doesn't that third party offer any kind of API for that ?
If so, a possible workaround would be to use QNetworkAccessManager to check whether the wanted host is reachable before starting the connection through your third party.
@SGaist thank you so much! Checking host reachability before trying to connect is perfect solution for me.