Polling of available printers in now asynchronous (#264,#256)

- Increased polling interval from 1s to 10s
- Use QtConcurrent::run to initiate a poll asynchronously to main event loop
- Do not initiate a poll if the previous poll has not completed
- Use copies instead of references to mCurrentAvailablePrinters in public API
This commit is contained in:
Jaye Evins
2025-12-09 09:22:54 -05:00
committed by GitHub
parent b6cac2d208
commit f1e50d8574
6 changed files with 43 additions and 10 deletions
+23 -3
View File
@@ -21,8 +21,10 @@
#include "PrinterMonitor.h"
#include <QPrinterInfo>
#include <QDebug>
#include <QMutexLocker>
#include <QPrinterInfo>
#include <QtConcurrentRun>
namespace glabels
@@ -39,11 +41,13 @@ namespace glabels
///
PrinterMonitor::PrinterMonitor()
{
using namespace std::chrono_literals;
mCurrentAvailablePrinters = QPrinterInfo::availablePrinterNames();
mTimer.reset( new QTimer( this ) );
connect( mTimer.get(), SIGNAL(timeout()), this, SLOT(onTimerTimeout()) );
mTimer->start( 1000 );
mTimer->start( 10s );
}
@@ -64,8 +68,9 @@ namespace glabels
///
/// Get available printers
///
const QStringList& PrinterMonitor::availablePrinters() const
QStringList PrinterMonitor::availablePrinters()
{
QMutexLocker mutex( &mCurrentAvailablePrintersMutex );
return mCurrentAvailablePrinters;
}
@@ -74,10 +79,25 @@ namespace glabels
/// On timer timeout
///
void PrinterMonitor::onTimerTimeout()
{
// Make sure previous poll is complete before starting a new one
if ( mPollStatus.isFinished() )
{
mPollStatus = QtConcurrent::run( &PrinterMonitor::asyncPoll, this );
}
}
///
/// Asynchronous poll
///
void PrinterMonitor::asyncPoll()
{
auto newAvailablePrinters = QPrinterInfo::availablePrinterNames();
if ( newAvailablePrinters != mCurrentAvailablePrinters )
{
QMutexLocker mutex( &mCurrentAvailablePrintersMutex );
mCurrentAvailablePrinters = newAvailablePrinters;
emit availablePrintersChanged( mCurrentAvailablePrinters );