Re: Cancel button or Status bar

Edgar Friendly <[email protected]>
Newsgroups gmane.comp.lang.ocaml.lib.gtk
Message-ID <[email protected]>
Angela Zhu wrote:
> Hi,
> 
> I am using OCaml to develop an interpreter with a GUI.
> In the GUI, I have a "run" button, where when you click it, it
> will call a function, say "eval prog1".
> 
> Some time, the running time of "eval prog1" can be long, and
> in case I want to add to the GUI the functionality that,
> when click on another "cancel" button, it will terminate the
> computation/evaluation immediately. (The user might don't
> want to wait until the execution to finish. )
> 
> Anyone have some idea how to achieve this?
> 
Many ways:
1) threads
2) break your mega computation into something that can get done in
pieces and have [eval prog1] return true or false based on whether it
has more work to do.  Then schedule it using [Idle.add], and it'll keep
running in chunks until it's done, but the GUI will still get time to
process events (and allow you to kill the task by removing it from the
idle loop).
3) like 2 but in reverse: in your computation, occasionally call
[Glib.Main.iteration false] to give GTK control of things to check its
signal handlers and see if it needs to cancel things (setting a global
variable or something, I guess)

> If this is impractical in OCaml, then how can I generate a
> "status bar", which indicates how much of the program
> (e.g. in percentage) has been finished, so you can have
> an estimation on how much more you need to wait for the program
> to finish.
> 
> Thanks a lot in advance!

Alternatively, you could make a progress bar ([GRange.progress_bar]),
and pass it to your function to call the [#set_fraction] method with a
float 0.0-1.0 to indicate % done.

E
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.