Re: Thread freezes gui after return



Dnia 2006-08-23 (środa) o 05:05:06 tboloo <azath o2 pl> napisał(a):

> By the way if I turn -Wall g++ switch it gives me error :
> 
> "cvthreadprocessor.cpp: In member function `void
> CvThreadProcessor::button_dilate_clicked()':
> cvthreadprocessor.cpp:105: warning: unused variable 'thread' "
Why do you have this variable thread if you don't wan't to use it ? 

> so I guess that there is something wrong with the way I create threads, but
> I can't figure out what's exactly wrong.
You are calling your ::repaint() method from thread and here you are using
Gtk::Image::set(), so you are drawing Gtk functions from your thread which is
not allowed.
You should you Glib::Dispatcher, to send signal to GUI and draw from here.

After adding Dispatcher your program is working normally - You can see it in
attached diff.
diff -ur cv_threads.orig/cvthreadprocessor.cpp cv_threads/cvthreadprocessor.cpp
--- cv_threads.orig/cvthreadprocessor.cpp	2006-08-23 13:53:09.000000000 +0200
+++ cv_threads/cvthreadprocessor.cpp	2006-08-24 09:36:04.000000000 +0200
@@ -16,6 +16,8 @@
     	m_Erode->signal_clicked().connect(sigc::mem_fun(*this, &CvThreadProcessor::button_erode_clicked));
     	m_Open->signal_clicked().connect(sigc::mem_fun(*this, &CvThreadProcessor::button_open_clicked));
     	m_Quit->signal_clicked().connect(sigc::mem_fun(*this, &CvThreadProcessor::button_quit_clicked));
+
+		m_signal_finished.connect(sigc::mem_fun(*this, &CvThreadProcessor::repaint));
 }
 
 CvThreadProcessor::~CvThreadProcessor()
@@ -29,7 +31,7 @@
 	time_t cur_time = clock();
 	while( (clock()-cur_time)/CLOCKS_PER_SEC < 2 )
 		;
-	repaint();
+	m_signal_finished.emit();
 }
 
 void CvThreadProcessor::thread_erode()
@@ -38,7 +40,7 @@
 	time_t cur_time = clock();
 	while( (clock()-cur_time)/CLOCKS_PER_SEC < 2 )
 		;
-	repaint();
+	m_signal_finished.emit();
 }
 
 void CvThreadProcessor::button_open_clicked()
@@ -84,6 +86,7 @@
 
 void CvThreadProcessor::repaint()
 {
+	std::cout << "Rysowanie " << std::endl;
 	Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_data( (guint8*)m_Frame->imageData,
                                                                            Gdk::COLORSPACE_RGB,
                                                                            false,
Tylko w cv_threads: .cvthreadprocessor.cpp.swp
diff -ur cv_threads.orig/cvthreadprocessor.h cv_threads/cvthreadprocessor.h
--- cv_threads.orig/cvthreadprocessor.h	2006-08-23 13:26:13.000000000 +0200
+++ cv_threads/cvthreadprocessor.h	2006-08-24 09:34:48.000000000 +0200
@@ -28,4 +28,5 @@
 		Gtk::Button* m_Erode;
 		Gtk::Image* m_Image;
 		IplImage* m_Frame;
+		Glib::Dispatcher m_signal_finished;
 };


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]