Re: Troubles with signal_child_watch



You have reason this is a bad example, but using gtk_main() should  take care of this isn't?
I thought that If i no specified any loop it should use the default one, in my code the timeout
signal works fine, but not the signal_child_watch().

Thanks.

On Sun, Sep 28, 2008 at 11:07 AM, Armin Burgmeier <armin arbur net> wrote:
On Sat, 2008-09-27 at 23:42 +0200, Fernando Tarín wrote:
> Hi I'm using the next code to launch a new process but the
> finished_process function is never executed, can someone help me with
> this issue?

You don't run a mainloop. All timeout handlers, idle handlers and child
watches are called from the glib mainloop. If you expect the child watch
to be called within the five seconds your code currently sleeps, maybe
try:

bool on_timeout(const Glib::RefPtr<Glib::MainLoop>& loop)
{ loop->quit(); return false; }

[...]

Glib::RefPtr<Glib::MainLoop> loop(Glib::MainLoop::create());
Glib::signal_timeout().connect(sigc::bind(sigc::ptr_fun(&on_timeout),
loop), 5000);
loop->run();

> #include <glibmm.h>
> #include <iostream>
>
> #include <unistd.h>
>
> void finished_process(int pid, int ret)
> {
>     std::cout << "Process finished: " << pid << "|" << ret <<
> std::endl;
> }
>
> int main()
> {
>     int stdout_fd, pid;
>     Glib::ustring line;
>     std::vector<std::string> command;
>
>     command.push_back("ls");
>
>     try{
>         Glib::spawn_async_with_pipes(".", command,
>             Glib::SPAWN_SEARCH_PATH, sigc::slot<void>(),
>             &pid, NULL, &stdout_fd, NULL);
>     }
>     catch(Glib::SpawnError &e){
>         std::cout << "Error: " << e.what() << std::endl;
>         return 0;
>     }
>
>
> Glib::signal_child_watch().connect(sigc::ptr_fun(finished_process),
>                        pid);
>
>     Glib::RefPtr<Glib::IOChannel> io =
> Glib::IOChannel::create_from_fd(stdout_fd);
>
>     sleep(5);
>
>     while (io->read_line(line) != Glib::IO_STATUS_EOF)
>                     std::cout << line;
>
>     io->close();
>     close(stdout_fd);
>     std::cout << "Exit" << std::endl;
> }

Armin





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