Re: How to handle long I/O operations



On Mon, 13 Aug 2001 11:18:56 +0200, Jean-Christophe Berthon said:

     I'm interested in this behaviour also as we will have a callback which
 call a really long calculus (can take hours) And it would be interessant to
 see a progress or activity bar. And as we will need to function under many
 different Linux and UNIX, we cannot use pthreads (I've heard that they were
 suffering some portablilty issues).
 
     So your solution is interesting me. But I didn't really understood it
 (I'm quite new to Gtk). So one first question that might enlight me : When
 you create a timeout (using gtk_timeout_add) with a period of a second for
 example, if another callback which will take let say about a minute is
 fired. Does the callback attached to the timeout is fired still every second
 (while the other is computing at the same time) or does it wait to be fired
 until the other callback ends?
 
     That will be all for the moment, once I understand this part, I might
 have other question. We will see :-)

There's no reason to use threading, atleast it won't be an advantage
on your case.

Let me give you an example, maybe that'll clarify the situation.
First lets say your situation is to load a BIG 100 MB file on
a 100 MB slow media (say a 100 MB floppy).

This would take a long time right? so not only will you want to
use a progress bar, but you'll probably want o manage GTK+ resources
or your own resources in the mean time. So set up your
program as follows:



/* This is yer job structure.
typedef struct {

        gchar *filename;
        size_t bytes_read;
        size_t length;          /* Size of file in bytes. */
        FILE *fp;
        GtkWidget *pb;          /* GtkProgressBar. */

} job_struct;
job_struct **job;                       /* Globals for this example. */
int total_jobs;

/* This is yer timeout callback. */
gint manage_tocb(gpointer data)
{
        gint i, bytes_read;
        job_struct *job_ptr;

        for(i = 0; i < total_jobs; i++)
        {
            job_ptr = job[i];
            if(job_ptr == NULL)
                continue;

            if(job_ptr->fp == NULL)
                continue;

            /* Load a segment of data. */
            bytes_read = fread(somedata, job_ptr->fp);
            if(bytes_read > 0)
            {
                /* Handle said data here. */


                /* Increment counter. */
                job_ptr->bytes_read += bytes_read;
            }

            /* Update progress bar here. */
            if(job_ptr->pb != NULL)
            {




            }

            /* Job done? */
            if(job_ptr->bytes_read >= job_ptr->length)
            {
                /* Deallocate job structure and mark it as NULL in
                 * the array.
                 */
                fclose(job_ptr->fp);
                g_free(job_ptr->filename);
                g_free(job_ptr);
                job[i] = job_ptr = NULL;        /* Mark it NULL. */
            }
        }

        return(TRUE);
}


/* This is your `start job' callback. */
void start_job_cb(GtkWidget *widget, gpointer data)
{
        /* Open file and create a new job structure here, append
         * it to the global job list. I assume you know how to
         * handle a pointer array that points to structures.
         * Remember to check for NULL pointers in the array first,
         * use them for a new structure if you find one so you
         * save memory.
         */
}

/* This is main, look it uses a pointer array for arguments.
 * So pointer arrays are cool. :)
 */
int main(int argc, char *argv[])
{

        gtk_init(&argc, &argv);

        /* Create a button or whatever to start a job. */
        /* Connect it to job_start_cb(). */

        gtk_timeout_add(100, manage_toid, NULL);

        gtk_main();

        return(0);
}


-- 
--
Sincerely,                  ,"-_                         \|/
-Capt. Taura M.             ,   O=__                    --X--
.__                          ,_JNMNNEO=_                 /|\
OMNOUMmnne.                  {OMMNNNEEEEOO=_
UOOOBIOOOEOMMn.               'LONMMMMNNEEEOOO=.__..,,..
UUOOEUUOOOOOOOObe              '"=OMMMMWNEEEOOOOO,"=OEEEOO=,._
OOUUUIEEIOONNOIUbe.                "7OMMMMNNNNNWWEEEEOOOOOO"   "'.
EEBNNMMMNWNWWEEIMMNe.             __  7EMMMNNNNNWWWEEEEEEEOO.     " .
NNMMMMWWWMMMWEINMMMNn            "=BBEEEEMMMMMMMMNNNWWWEEOOOOO=._     .
                  http://furry.ao.net/~learfox/






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