Click to See Complete Forum and Search --> : threading (programming) question


GaryJones32
08-01-2003, 12:08 PM
thanks again for the invaluable help as i continue to get in over my head.

i wrote this and it works but causes my gui to totally freeze till it's done

if someone could just point me in the right direction :)
it seems like these things are too woven together
to put half of it in a new thread ????
but i can figure that out if that's the answer....
i'm assuming popen is not the problem because of what you guys were telling me about child proccesses.

FILE *ptr;
FILE *pFile;
pFile = fopen ("outputFile.txt" , "w");
if ((ptr = popen(command, "r")) != NULL)
while (fgets(buf, BUFSIZ, ptr) != NULL)
fprintf(pFile,buf);
fclose(pFile);

Strogian
08-01-2003, 02:02 PM
I'm not sure about fixing your problem, but this:
fprintf(pFile,buf);

is a no-no. If buf happens to contain, for instance, "%s", then you've got a problem. ;) You should do this instead:
fprintf(pFile, "%s", buf);
or probably better:
fputs(pFile, buf);

tecknophreak
08-01-2003, 06:17 PM
I'm guessing that your snippet of code there takes a while. When you are in a call back, the gui won't repaint the screen, so this will cause the screen to freeze.

There's two ways of getting around this, one is to create another program which does just this. If you need to know how that program is doing, you can use either shared memory or some sort of internal communcations.

The second way is the thread method, which would be a lot easier. The processes automatically share global variables, so you don't have to worry about that.

Answer the question at all? You probably could speed it up even more then by placing the read in one thread and the write in another.

Stuka
08-01-2003, 06:30 PM
Damn..thought I replied to this earlier - change your fclose() call to pclose() first!

GaryJones32
08-02-2003, 02:02 PM
Originally posted by Stuka
Damn..thought I replied to this earlier - change your fclose() call to pclose() first!


Damn.... calling pclose() immediatly after calling popen() doesn't address the problem at all

it does however cause the outputFile.txt to be empty rather than full of output.

calling pclose right before fclose produces exactly the same results as not calling it at all -- it should be in there but has nothing to do with my question........ Damn..........

GaryJones32
08-03-2003, 12:23 AM
thanks technophreak,

it workes fine within a new thread
made a timer to listen for when the thread finishes...

for some reason i didn't think i could do that because of the new process..

that's what i get for trying to think instead of just do

Stuka
08-04-2003, 12:44 PM
GaryJones32: pclose() should be called for any FILE* opened by fclose() (per the man page), but it may not be the real reason for those issues.

tecknophreak
08-04-2003, 01:19 PM
Originally posted by Stuka
GaryJones32: pclose() should be called for any FILE* opened by fclose() (per the man page), but it may not be the real reason for those issues.

opened by popen instead of fclose.

fopen, fclose - open and close of file
popen, pclose - open and close of pipe

Stuka
08-04-2003, 03:49 PM
yeah...I was in a hurry. :rolleyes:

tecknophreak
08-04-2003, 04:18 PM
didn't mean to step on your toes as it were, i just didn't want anyone reading this to get confused. :)

GaryJones32
08-06-2003, 01:23 AM
Originally posted by Stuka
GaryJones32: pclose() should be called for any FILE* opened by fclose() (per the man page), but it may not be the real reason for those issues.


allas -- we degenerate into the absurdly obvious
but with what's on first and who's on second for a new twist