Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Interrupt 3rd party endless function
QtWS25 Last Chance

Interrupt 3rd party endless function

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 423 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nevdokimof
    wrote on last edited by
    #1

    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 calls connect (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);
    }
    
    aha_1980A 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply
      4
      • N nevdokimof

        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 calls connect (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);
        }
        
        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @nevdokimof

        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

        Qt has to stay free or it will die.

        1 Reply Last reply
        2
        • SGaistS SGaist

          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.

          N Offline
          N Offline
          nevdokimof
          wrote on last edited by
          #4

          @SGaist thank you so much! Checking host reachability before trying to connect is perfect solution for me.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved