Re: I have a little problem with gtkmm and threads



On Mon, 21 Aug 2006 12:22:49 -0500, Cesar Flores wrote:

> **
> 
> Excume for my bad english
> 
> Excuse me for my bad English
> 
> 
>  I have a problem that I think is simple for experienced developers but not
> for a beginner like me.
> 
> 
>  The think that i want to do is the next:
> 
> 
>  I have a very big code and I want to include a little window (made in
> gtkmm) in it. The window will present the results at any time of my code.
> 
> 
>  The problem is that when i want run the window with Gtk::Main::run(window)
> my code stop until I close the window. Then my window don't present any
> result. And the thing that I want is that my window runs without stop my
> principal code.
> 
> 
>  I tried with pthreads the next code:
> 
> 
>     Gtk::Main kit();
>    ExampleWindow window;
> 
> 
>    pthread_t threadWindow;
> 
>    pthread_create(&threadWindow,NULL,Gtk::Main::run,window); // create threads
>    my code continue here
> 
> And logically don't works. I read many faqs but alls are very complicated
> for the thing that i want to do. I somebody can give me a clear example I
> will apreciate it.
> 
> 
> 
> 
> César Flores
> <div class="mhl"><br><b><span class="nw" id="_user_gtkmm-list gnome org"></span></b></div><br>Excume for my bad english<br><br>
> 
> 



Some general advice on this:

You create a GUI derived from Gtk::Window

class MyGUI : public Gtk::Window
{
   // 
};

in which you put at least a Gtk::DrawingArea and Gtk::Button
(or maybe a Gtk::ToggleButton) to start and stop the thread.
I'm assuming the results you want to display are things like
curves, or geometric shapes, or points. If it's numbers or text,
you don't need a gui, do you? Just output them to stdout.

The easy way to create the gui is with glade-2. 



Your Darea will have a callback like on_Darea_expose_event()
which gets called when the Darea must be re-drawn (the expose
event). Here you draw all your stuff.

In the button callback you create a work thread. Use Glib::Threads
for all this. The documentation is here:

 http://www.gtkmm.org/docs/glibmm-2.4/docs/reference/html/group__Threads.html

and you can find two examples at the bottom of the page.


Your results are being computed in the work thread, and you 
want them drawn into the DrawingArea. The way to do that is
not to draw them directly from the working thread, but to
use a Glib::Dispatcher to tell the GUI thread to do the drawing.
See the dispatcher.cc example at the bottom of the Threads page.
In your case, you must connect the dispatcher to 
DrawingArea::queue_draw(). It will make sense when you read
the example and the documentation. 


I don't mean to discourage you, but don't expect this to be easy. 
If you think this framework is suitable for what you need to do,
(so far I'm only guessing), and if you can't figure it out on
your own, I'll dig up some code. As far as I know there is only
one example on Glib::Dispatcher, and no example that shows 
how to draw into a DrawingArea from a non-gui thread. It took
me a lot of time and trial and error, and a lot of help from the
people in this mailing list.

If this is what you want to do, I'll post an example.





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