Di seguito è riportato il codice per la mia implementazione qthread. Sto cercando di ottenere i dati GPS dal satellite. QThread non produce il segnale finished() anche quando i programmi escono dalla funzione slot gpsSearch()
. La funzione locateMe()
viene richiamata ogni volta che si fa clic su un pulsante. La prima volta che il thread non viene avviato e si fa clic sul pulsante, viene stampato il valore vero per la funzione isRunning()
e viene stampato il valore falso per la funzione isFinished()
. Ho dovuto chiamare la funzione quit()
di QTherad per interrompere manualmente il thread. Dopodiché passa alla funzione threadQuit()
connessa nella classe gnssProvider
. Ma anche dopo, se clicco sul pulsante, stampa vero valore per isRunning
e false per isFinished()
nella funzione locateMe()
.QThread emette il segnale finished() ma isRunning() restituisce true e isFinished() restituisce false
GPSInfo::GPSInfo()
{
hybridGPSFound = satelliteGPSFound = networkGPSFound = false;
qDebug()<<"Thread Creating";
gnssThread = new QThread;
gnssProvider = new LocationFetcher(this,GEOLOCATION_PROVIDER_GNSS,1);
gnssProvider->moveToThread(gnssThread);
connect(gnssThread, SIGNAL(started()), gnssProvider, SLOT(gpsSearch()));
connect(gnssThread, SIGNAL(finished()), gnssProvider, SLOT(threadQuit()));
}
void LocationFetcher::gpsSearch()
{
if (BPS_SUCCESS != geolocation_request_events(0))
{
fprintf(stderr, "Error requesting geolocation events: %s", strerror(errno));
return;
}
geolocation_set_provider(GPS_Search_Provider);
geolocation_set_period(GPS_Search_Period);
while (!stopThread)
{
bps_event_t *event = NULL;
bps_get_event(&event, -1);
if (event)
{
if (bps_event_get_domain(event) == geolocation_get_domain() && bps_event_get_code(event) == GEOLOCATION_INFO)
{
handle_geolocation_response(event);
break;
}
}
}
geolocation_stop_events(0);
this->quit();
}
void GPSInfo::LocateMe()
{
qDebug()<<"Thread Running: "<<gnssThread->isFinished();
qDebug()<<"Thread Running: "<<gnssThread->isRunning();
gnssThread->start();
hybridThread->start();
networkThread->start();
}
Sei sicuro che non hai confuso isFinished e isRunning? Hai '" Thread Running: "' in entrambe le righe. –
No, non l'ho fatto :) Ero troppo pigro per risolvere il problema: P – Tahlil
puoi pubblicare un esempio completo? Forse thread non ha finito –