Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run a tcl/tk procedure in background ?

Status
Not open for further replies.

charmdream

Programmer
Oct 26, 2001
25
0
0
US

Is there any method that I can run a tcl/tk procedure (may take long time) in background, so that other procedures can be executed at the same time?

Thanks
 
I think the answer depends on what O/S you're using and what you mean by running in the background. Let's say your on UNIX and your script runs as a command line executable:
> scriptname
In that case, you can invoke it as a background job:
> scriptname &
[I'm sure you know that so pardon the trivial first case]

On Windows, you can invoke your script and then invoke another script or executable while the first one is running. Again, somewhat trivial.

If, however, what you mean is that you want to run a proc or script portion and not wait for it to finish before you go on to another portion of the same script, that is not trivial. In fact, the only reason you would be doing that is because you want the 2 processes to communicate. In that case you probably want to open channel and use "fileevent" to mediate the communication. [From the Tcl Help file: "This command is used to create file event handlers. A file event handler is a binding between a channel and a script, such that the script is evaluated whenever the channel becomes readable or writable. File event handlers are most commonly used to allow data to be received from another process on an event-driven basis, so that the receiver can continue to interact with the user while waiting for the data to arrive. If an application invokes gets or read on a blocking channel when there is no input data available, the process will block; until the input data arrives, it will not be able to service other events, so it will appear to the user to ``freeze up''. With fileevent, the process can tell when data is present and only invoke gets or read when they won't block. "] Bob Rashkin
rrashkin@csc.com
 
Thanks for your reply.

What I want is as following:
There is a file(maybe huge). I want to open it and convert each line into some format and then display it using text widget.

It seems that the display will not be ready until the procedure(read file, convert format and insert the text into text widget) is finished.

Could anyone tell me whether there is some other method that can do it without waiting?

thanks
 
No, you've got it right. I've run into the same problem and the best I could do is reset the cursor to "watch" (i.e. hour glass) until the text is rendered. I think that if it's a real problem, you would want to right a file reader in C and make it a dll. Bob Rashkin
rrashkin@csc.com
 
That's a bad news for me. :-(

Anyway, thank you very much!

 
May be this is convenient for you?
Code:
set h [open a_huge_file]
pack [text .text]

while {![eof $h]} {
  set line [gets $h]
  # some extra work with line
  .text insert end $line\n
  .text see end
  update
}

close $h
The update command lets the text widget to scroll.
But with that you can't do another task in parallel if this is wanted.

ulis
 
Thanks a lot, Ulis.

It looks good and is helpful. :)
 
Now I can use 'after' command to make the time-consuming work do periodicly. But the main program is not stuck there. And it looks good.

proc processfile {file} {

# do some processing(maybe 100 lines or more)
# and display

if {$file not end} {
after 10 "processfile $file"
}
}

proc main {} {
# do other things
after 10 "processfile a-huge-file"

}

Thanks for your good ideas.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top