[Qt-interest] Plugin design with a polling thread

Mandeep Sandhu mandeepsandhu.chd at gmail.com
Fri Mar 19 06:45:11 CET 2010


Hi All,

I have a requirement in my application (a plugin), where it needs to
"poll" a particular h/w for results of command given to it.

The plugin accepts commands from an ext app and issues it to the h/w.
The results of these cmds are not immediately evident and are
available only after some delay (~500-3000msecs). Hence the need for
polling.

To this effect I create a separate thread for polling the h/w whenever
a new cmd is issued. This design is similar to the Mandelbrot example
shown here: http://qt.nokia.com/doc/4.5/threads-mandelbrot.html (See
the RenderThread).

Now the way to test for whether a command passed or failed, is
different for different cmds. That is why I cannot have a common
"test" method implemented in my thread. Rather I want to put all h/w
specific functions in the main plugin class since thats where all the
logic is. The poll thread should be generic, in that it only accpets
params like number of times it has to poll, the polling interval and a
"Test function" which it will call to test for the cmd's status.

So, if later someone adds a new cmd, he/she can implement it's
associated test fxn and pass this name to the poll thread which will
call it each time it runs.

For this I basically need to invoke a method defined in the plugin
class from this polling thread. There'll be only one instance of the
polling thread and the plugin class makes sure that a second cmd is
not issued till results of the previous cmd is available.

One way I can think of is to use the QMetaObject::invokeMethod
property with Qt::DirectConnection (since I want the result
immediately) and call the test fxn in the plugin class (an instance of
the plugin class is available with the polling thread).

If the test succeeds, the poll thread "emits" the result and goes to
sleep, if not it calls this test fxn again after the specified
interval (provided the number of retries as not been exceeded).

Since its running in an embedded env, wanted to know if there are
simpler ways of doing this?

Your thoughts.

Thanks,
-mandeep



More information about the Qt-interest-old mailing list