Poniżej znajduje się kod implementacji qthread. Próbuję uzyskać dane GPS z satelity. QThread nie generuje sygnału finished() nawet wtedy, gdy programy opuszczają funkcję automatu do wstawiania gpsSearch()
. Funkcja locateMe()
jest wywoływana po kliknięciu przycisku. Za pierwszym razem, gdy wątek nie zostanie uruchomiony, a przycisk zostanie kliknięty, drukuje on prawdziwą wartość dla funkcji isRunning()
i wypisuje fałszywą wartość dla funkcji isFinished()
. Musiałem zadzwonić do funkcji QTherad quit()
, aby ręcznie zatrzymać wątek. Następnie przechodzi do połączonej funkcji threadQuit()
w klasie gnssProvider
. Ale nawet po tym, gdy kliknę przycisk, wypisze on true value dla isRunning
i false dla isFinished()
w funkcji locateMe()
.QThread emituje gotowy() sygnał, ale funkcja isRunning() zwraca wartość true, a isFinished() zwraca wartość 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();
}
Czy jesteś pewien, że nie pomieszałeś isFinished i isRunning? W obu liniach mamy '' Thread Running: "'. –
Nie, nie zrobiłem :) Byłem zbyt leniwy, aby to naprawić: P – Tahlil
czy możesz zamieścić pełny przykład? Może wątek nie zakończył się. –