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. [SOLVED] Write Dll's for Heavy work
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Write Dll's for Heavy work

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 1.5k Views 1 Watching
  • 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.
  • A Offline
    A Offline
    antonio
    wrote on last edited by
    #1

    Hi,

    I know how to create a dll. I want to write a dll that runs a process and it doesnt block the main application. I tried with QThreadPool and QRunnable, but it crashes. Something like:

    @
    #include "MyDll.h"
    #include <QApplication>

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    MyDll dll;
    dll.heavyProcess();
    dll.heavyProcess();
    dll.heavyProcess();
    dll.heavyProcess();

    return app.exec();
    }
    @

    Any idea will be appreciated.

    Thanks

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      What about QtConcurent ?

      Also, it would be better to fix the crash

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

      1 Reply Last reply
      0
      • A Offline
        A Offline
        antonio
        wrote on last edited by
        #3

        Thanks...

        I've resolved like this:

        myDll.h

        @#ifndef myDll_h
        #define myDll_h

        #include <QObject>
        #include "myDllGlobal.h"

        class QThreadPool;

        class myDll_EXPORT myDll : public QObject
        {
        Q_OBJECT

        public:
        HttpLoggerDll(QObject *parent = 0);
        void heavyWork();

        private:
        static QThreadPool *threadPool;
        };
        #endif
        @

        myDll.cpp

        @
        #include "myDll.h"
        #include "MyThread.h"
        #include <QThreadPool>

        QThreadPool *myDll::threadPool = 0;

        myDll::myDll(QObject *parent) : QObject(parent)
        {
        threadPool = QThreadPool::globalInstance();
        }

        void myDll::heavyWork(){
        MyThread *thread = new MyThread();
        threadPool->start(thread);
        }
        @

        MyThread.h

        @
        #ifndef MyThread_h
        #define MyThread_h

        #include <QRunnable>
        #include <QObject>

        class MyThread :public QObject, public QRunnable{

        Q_OBJECT

        public:
        MyThread(QObject *parent = 0);
        ~MyThread();

        void run();
        

        };
        #endif
        @

        MyThread.cpp

        @#include "MyThread.h"

        MyThread::MyThread(QObject *parent) : QObject(parent) {}

        MyThread::~MyThread(){}

        void MyThread::run(){
        // heavy work
        }
        @

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          So you have it fixed ?

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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            antonio
            wrote on last edited by
            #5

            Yes. it works. Thanks

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What did you do to make it work ?

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

              1 Reply Last reply
              0
              • A Offline
                A Offline
                antonio
                wrote on last edited by
                #7

                Declaring my variable threadPool as static was the key

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Sounds rather like a workaround. Anyway, you shouldn't need to have that variable. You should rather use the "global instance":http://qt-project.org/doc/qt-5/qthreadpool.html#details

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

                  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