If the wait time to read a line isn't slow, yes, you can do that
technique.

But if the output is sporadic, with possible > .5 sec times,
you'd really want to probably start a child thread that
reads the file and updates an array common with the main thread
(using a mutex to protect the array) and have an FLTK timer that fires
every .10 sec or so which empties the array, appending it to the text widget.

The mutex lock would keep the two threads in sync.

ie:

child thread:

        while ( read file )
            mutex_lock(array)
            append file data to global array
            mutex_unlock(array)
        close file
        exit thread

parent fltk timer:

        mutex_lock(array)
        read text from global array, append to text widget, empty array
        mutex_unlock(array)
        reload timer to fire .10 secs from now

This would actually work on all platforms. And you could still use popen()
(in the child thread).

Shouldn't be hard. In Windows you can use 
EnterCriticalSection()/LeaveCriticalSection()
to manage the mutex lock.
On 6/23/11 1:18 PM, Jeff Paranich wrote:
> Hi All,
> 
> I'm trying to use a pipe and execute a command such that the stdout from it 
> is displayed in a text display box.  I've read Erco's tutorial on this but it 
> is unix based and this is for Win32.
> 
> The problem -as anyone would likely guess- is the failure to redraw the text 
> display in real-time.  Instead the data is displayed after the command is 
> executed.
> 
> I've tried fiddling with Fl:check() to no avail.  Anyways, some sample code 
> is below.  This function is called with a command and then the results pipe 
> out into db_log which is a Fl_Text_Display object.
> 
> std::string NewProjectWindow::exec(char* cmd)
> {
>     FILE* pipe = _popen(cmd, "r");
>     Fl_Text_Buffer *buff = new Fl_Text_Buffer();
> 
>     if (!pipe)
>       return "ERROR";
>     char buffer[128];
>     std::string result = "";
>     while(!feof(pipe)) {
> 
>         if(fgets(buffer, 128, pipe) != NULL)
>       {
>             result += buffer;
>           buff->text(result.c_str());
>           this->db_log->buffer(buff);
>           this->db_log->redraw();
>           Fl::check();
>       }
>     }
>     _pclose(pipe);
>     return result;
> }
> 
> Any help/pointers are greatly appreciated. :)
> 

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to