Hello everybody.
I have a little problem.
I have an app with two buttons, one for start an image capturing thread from a webcam and show them, and the another one is for stop thread (wich means stop capturing).
My thread's run method look like this:
void thread::run()
{
while(1)
{
webcam_read_frame(parent_thread->cam_fd, parent_thread->pixelImagen);
if(parent_thread->need_update)
{
QCustomEvent
*evento
= new QCustomEvent
(QEvent::User);
parent_thread->need_update = false;
}
}
}
void thread::run()
{
while(1)
{
webcam_read_frame(parent_thread->cam_fd, parent_thread->pixelImagen);
if(parent_thread->need_update)
{
QCustomEvent *evento = new QCustomEvent(QEvent::User);
parent_thread->need_update = false;
QApplication::postEvent(parent_thread, evento);
}
}
}
To copy to clipboard, switch view to plain text mode
The idea is the thread keep running 'till i push "stop" button.
The code for my stop buttons is:
void gui_tracksys::slot_pushButtonStop_clicked()
{
webcam_close(cam_fd); //this line stops capture and close webcam
//also delete other things like QImage, QPixmap, QPainter, etc...
if (myThread->running())
{
myThread->terminate();
myThread->wait(5000);
delete myThread;
}
}
void gui_tracksys::slot_pushButtonStop_clicked()
{
webcam_close(cam_fd); //this line stops capture and close webcam
//also delete other things like QImage, QPixmap, QPainter, etc...
if (myThread->running())
{
myThread->terminate();
myThread->wait(5000);
delete myThread;
}
}
To copy to clipboard, switch view to plain text mode
I see the problem is I keep calling webcam_read_frame() after I've called webcam_close(), because when i click "stop" the first thing i do is close webcam and thread still running, so the run method try to call webcam_read_frame() but cam doesn't exist anymore.
I've tried to stop thread first and then close webcam, but this cause my app becomes unresponsive and need to click "x" (window close button) and then click "force close" when asked.
I don't know how to fix my problem, so any suggestion is welcome.
I'm using QT3/c/c++
I'm sorry about my bad english... Thank you very much...
PS:If need more info about my code i will post, just let me know.
Bookmarks