GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 01, 2014 1:13 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: How to use g_spawn_command_line_sync ?
PostPosted: Fri Jul 03, 2009 3:25 am 
Offline
GTK+ Geek

Joined: Mon Nov 10, 2008 6:54 am
Posts: 64
I tried to create a simple IDE. Then when I tried to pass the command :
Code:
void compile_clicked( GtkWidget *w, TextEditor *editor )
{
   char          *output;
    GError       **error = NULL;
    GtkTextBuffer *buffer;
   
    string command_line =  "g++ main.cpp -o main";
   cout << command_line << endl;
   cin.ignore( 80, '\n' );

    g_spawn_command_line_sync( command_line.c_str(),
                               &output,
                               NULL,
                               NULL,
                               error );
   
    buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW( editor->error_textview ) );
    gtk_text_buffer_set_text( buffer, output, -1 );
}

It compiled fine, but after I click the compile_clicked button, my app was completely freeze :( ! I don't know why since which other command line such as "cat" or "find", it works really fine. Could anyone give me a hint where I did wrong ? Thanks in advance !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 03, 2009 7:08 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

It's normal for application to "freeze" when using sync version of spawning. g++ will take some time to complete entire operation, and in the mean time, your application will be frozen.

To get the idea how long your app will be frozen, try executing your command on a command line and check how much time it takes to finish (my guess would be that g++ will take couple of seconds to finish).

If you would like to avoid being frozen, use async version of spawning and read output directly from pipe in idle or timeout handler.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 03, 2009 2:37 pm 
Offline
GTK+ Geek

Joined: Mon Nov 10, 2008 6:54 am
Posts: 64
Thanks tadeboro !
Quote:
Hello.
If you would like to avoid being frozen, use async version of spawning and read output directly from pipe in idle or timeout handler.

I'm not sure I get this point, can you give me a minimal example ?
And another question is :
With shell we have that :
Code:
g++ main.cpp -o main 1> output.txt 2> error.txt

where :
+ output.txt will hold the result of standard output.
+ error.txt will hold the result of standard error.
Is there any function in GTK+ or GLib could deal with standard error ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 15, 2009 8:58 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

I wrote a quick introduction on spawning processes using glib's functions. Sample code also includes reading from stdout and stderr.

You can find it here: http://tadeboro.blogspot.com/2009/07/spawning-processes-using-glib.html


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 15, 2009 10:10 pm 
Offline
GTK+ Geek

Joined: Mon Nov 10, 2008 6:54 am
Posts: 64
Great :D ! Thanks a lot tadeboro !


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group