Re: How to update both the Console and the GTKMM gui window after running other functions



Ah.  Perhaps it's how you built it.  If you're using Linux, I link with -lgthread-2.0.  I actually like with -lpthread as well, although that might not be necessary.  I have multiple types of threads in some apps.  Try that and see if that helps.

-- Alan


On 8/5/2013 12:05 PM, L. D. James wrote:
Thanks, Alan.  It compiles without errors.  When run I get this error at the console:

        Threads aren't supported!

// c++ code begin
// -----------------------------------------------------------
#include <gtkmm.h>
#include <iostream>

using namespace std;

class myLabel: public Gtk::Window
{
// blah blah blah
public:
myLabel();
virtual ~myLabel();

protected:
Gtk::Label m_label;
string labeltext;
string newtext;
void myprocess1(void);
};

myLabel::myLabel() :
m_label("Hello World")
{
// myLabel.set_text("hello"); // This line gives errors (*1)
void myprocess1();

set_title("Gtkmm Programming - C++");
add(m_label);

m_label.show();

Glib::Thread::create(sigc::mem_fun(*this, &myLabel::myprocess1), true);

cout << "Window should be displayed" << endl;
}

myLabel::~myLabel()
{
}

void myLabel::myprocess1(void)
{
labeltext = "About to preform a number of processes.\n";
labeltext += "Each process may take up to three hours.\n";
labeltext += "Please carry your daily chores and wait.\n";
cout << labeltext << endl;
m_label.set_text(labeltext);

sleep(10); // Back from a three hour function
newtext = "Back from a three hour function\n";
labeltext += newtext;
m_label.set_text(labeltext);
cout << newtext << endl;

sleep(10); // Back from a three hour function
newtext = "Back from another three hour function\n";
labeltext += newtext;
m_label.set_text(labeltext);
cout << newtext << endl;
}

int main(int argc, char* argv[])
{
if (!Glib::thread_supported())
Glib::thread_init();
else
{
cerr << "Threads aren't supported!" << endl;
exit(1);
}

Gtk::Main kit(argc, argv);

myLabel mylabel;
Gtk::Main::run(mylabel);
return 0;
}
// -----------------------------------------------------------
// c++ code end

-- L. James

--
L. D. James
ljames apollo3 com
www.apollo3.com/~ljames

On Mon, 2013-08-05 at 08:18 -0700, Alan Mazer wrote:
Take a look at this.  It still doesn't have dispatchers (I'm trying to 
keep it simple).  It's possible for this program to crash if you try to 
interact with the window while it's updating.  Dispatchers prevent 
that.  But this should be otherwise functional, and I didn't want adding 
dispatchers to make understanding the thread concept more difficult.  
Adding dispatchers is just one more step.

All I did here was put your long-running computation into a secondary 
thread, and have that thread update your label.

The problem with your previous approach is that what "run" does is 
display the window and label you've created and then wait for you to 
interact with the window.  If you put your computation *before* the 
"run", the window won't be displayed until your computation ends. If you 
put your computation *after" the "run", it won't be executed until you 
close the window.

You can also put your computation *in* the window creation, but this 
means that the program won't be handling any user interactions during 
your computation.  If you cover the window with another window, for 
example, and then uncover it again, it will probably be blank until your 
computation is over.  Or if you had a button in the window, button 
presses would be ignored until the computation was over.

What you want is for your computation to be going on at the same time as 
"run" is handling user interaction, both of them working together.  So 
we create a "thread", which is a separate function that runs 
simultaneously with the "run".

I hope this helps.  You probably want to switch from a label to 
something with scroll bars.

-- Alan





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