• Re: Blocking ::http::geturl?

    From Rich@21:1/5 to Silas Silva on Wed Jan 3 23:45:12 2024
    Silas Silva <silasdb@gmail.com> wrote:
    Hello there!

    I've written a very simple Tk application that is very serial.
    Recently I added support for http requests like this:

    set token [::http::geturl example.com]
    set data [::http::data $token]
    (configure .toplevel.mywidget with $data)

    I soon discovered that ::http::geturl doesn't block the main loop,
    which is great, but it happens that for my very serial application,
    it can be problematic sometimes. For instance: sometimes I destroy
    the toplevel in which mywidget lives before it got a chance to
    receive $data, so when geturl resumes its execution, my code throws
    an error because it tries to configure mywidget.
    [...]
    Alternatively, is there a more elegant solution that is not to add a
    bunch of if's to check if widgets still exist?

    If it is just a missing toplevel, you could wrap the 'configure' in a
    try:

    try {
    configure .toplevel.mywidget with $data
    } on error {a b} {
    # do whatever you should do here when the configure fails
    }

    Or you can check for the toplevel before trying to configure it:

    if {[winfo exists .toplevel]} {
    configure .toplevel.mywidget with $data
    } else {
    # do what you need to do when the toplevel has been destroyed
    }

    Note that the "do whatever" should include freeing the return token
    from geturl, otherwise you will have a memory leak.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)