Is there a way to schedule a task to be executed in the Windows main loop. I don't want to create a Windows and send an event to it.
With libdispatch I can do it with:
dispatch_async_f(dispatch_get_main_queue(), ...)
Or is there a thread in the Windows Thread Pool associated with the program's main loop?
Background:
Currently I am in the process of developing a CSP (Communicating Sequential Processing) library and most of the time tasks can be executed by the Thread Pool. But sometimes there is the need that a task is executed in the main loop. I have already a solution, if the application under Windows is a Qt application. But for non Qt applications under Windows I would like to have a solution as well.
On Windows each message loop is associated with a single thread. You can have more than one message loop per application if this is what you ask.
Related
I have a utility which will access the database and clean the garbage files. this is a console application which is being called from another application.
I have a thread from the main thread which will do the tasks while the main thread will be in a loop to check if cancel of the utility has been called.
The functions in the thread call a different dll which takes time. Now, is there any way that when I cancel the thread. Even if it is inside the function, it gets notified to stop immediately and rollback any changes done to the database?
My application is a windows based application and is written in c++. I have tried using TerminateThread() function from Windows but the problem is that is does not allow to do clean up on the thread.
I need both event loops: one for Windows service (or Linux daemon) and another for Qt event queue QCoreApplication::exec() (or QApplication::exec() or even QEventLoop::exec()).
Can I have both at the same time in single thread? Or should I create a separate thread for one of them? In the latter case how should be arranged interaction process between QObjects and "window"/"service" thread?
Windows service requires either Message only window along with window procedure to receive and process a messages from the Windows, or Service Control Handler Function. I want to be able to process both kinds of events comes from the Windows and Qt-specific ones.
Can I use QEventLoop/QCoreApplication/QApplication::processEvents to process Qt events between events, that comes from a Windows? How can it affect service responsiveness and QTimer responsiveness?
Try to use QtService library. The QtService is useful for developing Windows services and Unix daemons:
https://github.com/qtproject/qt-solutions/tree/master/qtservice
Alternatively, you can realize it yourself like as in QtService library:
https://github.com/qtproject/qt-solutions/blob/master/qtservice/src/qtservice_win.cpp#L556
Qt event loop integrates native notifications/events on all platforms. The nativeEventFilter is how you react to native events when you wish to.
In my C++ application I'm using a third party library for Bluetooth discovering process. I'm looking at the examples provided to learn how to use it.
The example that best match my needs is a simple GUI application that call a Discovery(long timeout) function from the library to start the Bluetooth discovery.
That function returns immediatly (so that the GUI is not freezed) and fires an __event called OnDeviceFound once a new BT device has been discovered and OnDiscoveryComplete once the timeout has elapsed.
So in the GUI constructor (of the example) there're __hook defined like this:
__hook(&BluetoothDiscovery::OnDiscoveryComplete, &m_Discovery, &BluetoothClientDlg::OnDiscoveryComplete);
Now, I need to implement the same in my application, that is not a Window application but a console application that runs as a Windows Service, doing a continuos discovering on a separate thread looking for new devices.
So, actually, since my implementation makes use of a thread for discovery, I don't need an event based discovery procedure, but I need a blocking discovering. The library does not provide a blocking API for discovering.
So here comes the question: is it possible to use an event based function in a blocking function? In other words, is it possible to write a function that could be called in the thread main loop every n seconds that does a discovery procedure and return the founded Bluetooth devices (using that event-based library API)?
What you want is a Semaphore which your main thread sits on until the discovery thread completes and then notifies your main thread to wake.
Active waits like what you suggest are nasty, and should be avoided where you can.
I'm launching a bat file with system() in my software and it's can go to an infinit loop.
the question is how can I detect it in my cpp application ?
I'm using VS2010.
thanks
You can create a thread, and let the thread do the run of your batch file, and then set a timer with a timeout in the main thread to check whether the thread has ended its execution. If it takes longer than the timeout period, stop it and claim that it has an infinite loop.
I don't see any other way, because you practically can't access the batch file.
For threads, you may use boost threads or Qt threads, and there's many more different libraries for threads.
My embedded project contains a Qt application for the PC which is mainly a simulator for debugging and testing. In the application I can create several widgets which represent either my embedded software or simulate the hardware controlled by the application or can generate external inputs for testing.
I plan to improve the application by adding Lua scripting so that the widgets can be created or controlled from a script. I need an elegant way for single stepping the scripts.
I plan scripts like:
createThermometerWidget(10,20,30)
while time < maxTime do
setTemperature(20+time/1000)
pauseSimulation()
time = time + 1
end
The custom function pauseSimulation should stop the Lua script, enable the Qt event loop to run so that interaction with the software is possible (seting other inputs for example) and after pressing a button the script will continue.
My first idea was to create a separate thread for the Lua execution which will be stopped by pauseSimulation and released by the button. But Qt widgets cannot be created from non-main thread so that I will have to create all widgets in the main thread and pass all constructor parameters from Lua functions to the main thread.
Is there a more smooth way?
Coroutines are one approach to implementing this. Your pauseSimulation() can internally call coroutine.yield(), and be restarted later by a call to coroutine.resume() from the button's action. The problem is that your UI is at the mercy of your script fragments, since the only way to halt a running coroutine is for it to eventally call yield().
Alternatively, you can use the Lanes module to put part of your Lua application into a separate thread. You would use a Linda to pass messages from the main Qt widget thread to your simulator's worker thread. This would have the advantage that the UI thread is not blocked by the simulation which runs in its own thread.